我试图在点击一半屏幕后更改节点的位置。 我有三个确切的点,我希望节点在它们之间移动。为了检测节点的位置,我创建了BOOL' s。
我的代码无法正常工作。如果节点位于第3点,我点击屏幕左侧,它将转到第1点而不是第2点。 如果我将更新方法更改为Node.position.x ==(self.frame.size.width / 2),则节点仅在第1点和第3点之间移动。
@implementation MyScene {
BOOL Point1;
BOOL Point2;
BOOL Point3;
}
-(void)Node {
Node = [SKSpriteNode spriteNodeWithTexture:Node1];
Node.size = CGSizeMake(50, 50);
Node.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3);
Node.zPosition = 4;
[self addChild:Node];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
// If left half of the screen is touched
if (touchLocation.x < self.size.width / 2) {
if(Point2 == YES){
Node.position = CGPointMake(self.frame.size.width / 3.5, self.frame.size.height / 3);
}
if(Point3 == YES){
Node.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3);
}
}
// If right half of the screen is touched
if (touchLocation.x > self.size.width / 2) {
if(Point1 == YES){
Node.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3);
}
if(Point2 == YES){
Node.position = CGPointMake(self.frame.size.width / 1.4, self.frame.size.height / 3);
}
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
if(Node.position.x >= (self.frame.size.width / 1.4)){
Point1 = true;
Point2 = false;
Point3 = false;
}
else if(Node.position.x >= (self.frame.size.width / 2)){
Point1 = false;
Point2 = true;
Point3 = false;
}
else if(Node.position.x >= (self.frame.size.width / 3.5)){
Point1 = false;
Point2 = false;
Point3 = true;
} }
答案 0 :(得分:0)
更新方法中的逻辑错误。每当Node的位置大于self.frame.size.width / 2
Point2为真。以下是UpdateMethod
的工作代码:
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
if(Human.position.x > (self.frame.size.width / 1.6)){
Point1 = false;
Point2 = false;
Point3 = true;
}
else if(Human.position.x == (self.frame.size.width / 2)){
Point1 = false;
Point2 = true;
Point3 = false;
}
else if(Human.position.x < (self.frame.size.width / 3.0)){
Point1 = true;
Point2 = false;
Point3 = false;
}
}