【问题标题】:How to use Touches Began properly with CGPoint如何使用 CGPoint 正确使用 Touches Begin
【发布时间】:2014-09-25 07:50:51
【问题描述】:

这里的目标是,当我点击 UIImageview 右侧或左侧的屏幕时,Int 侧移动会更改以创建向左或向右移动。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];

 CGPoint touchLocation = [touch locationInView:self.view];


if (image.center.x > touchLocation) {
    sidemovement = -2;

}
if (if (image.center.x < touchLocation) {
    sidemovement = 2;)



[self jump];



}

问题是我希望将 CGpoint 触摸位置与 image.center.x 位置进行比较,并以此确定图像应该向左还是向右移动?

谢谢!

【问题讨论】:

    标签: objective-c touchesbegan


    【解决方案1】:

    我是 Objective-C 的新手,但也许其中的一些内容可能会对您有所帮助,如果我误解了您的问题,我深表歉意。

    您是否要左右移动图像?如果是这样,您必须先创建一个 CCPhysicsNode,然后确保图像是 CCNode,然后将图像添加到 CCPhysicsNode。

    你会这样做

    CCPhysicsNode *physicsNode;
    CCNode *image;
    

    然后在你的 init 或任何你想将图像放入物理节点的方法中调用它

    [physicsNode addChild:image];
    

    现在,您可以调用它来向左或向右移动图像(PS,我不确定您的 image.center.x > touchLocationn 是否有效。但假设它是....)

    if (image.center.x > touchLocation) {
     [image.physicsBody applyImpulse:ccp(-2.f, 0.f)];
    
    }
    if (image.center.x < touchLocation) {
     [image.physicsBody applyImpulse:ccp(2.f, 0.f)];
     [self jump];
    
    }
    

    您的示例代码中还有额外的 if 和括号。
    希望这能解决您的问题……如果不是,那么……写起来很有趣!如果有人发现我输入的内容有错误,请告诉我 - 我还在学习中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 2012-07-12
      • 2013-01-21
      相关资源
      最近更新 更多