这里的目标是,当我点击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位置进行比较,从而确定图像应该向左还是向右移动?
谢谢!
发布于 2014-08-02 17:37:04
我是Objective-C的新手,但其中一些可能会对您有所帮助,如果我误解了您的问题,我道歉。
您是否正在尝试左右移动图像?如果是这样,您必须首先创建CCPhysicsNode,然后确保镜像是CCNode,然后将镜像添加到CCPhysicsNode。
你会这样做的
CCPhysicsNode *physicsNode;
CCNode *image;然后在init中调用它,或者调用您想要将图像放入physics节点的任何方法
[physicsNode addChild:image];现在,您可以调用它来向左或向右移动您的图像(附注:我不确定您的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和圆括号。
希望这篇文章不是针对你的question...if,well...it写的很有趣!如果有人发现我输入的内容有错误,请让我知道--我还在学习。
https://stackoverflow.com/questions/25091577
复制相似问题