[英]Sprite node won't move when touch is recognized in Sprite Kit
我試圖讓用戶在屏幕上的某個點上方觸摸時移動一個精靈節點。 我的觸摸被識別,但是沒有任何動作發生!
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.anchorPoint = CGPointMake(0.5, 0.5);
SKSpriteNode *_box = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
_box.position = CGPointMake(0.5,0.5);
[_box addChild:_box];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
_currentTouch = touch;
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
CGPoint currentLocation = [_currentTouch locationInView:_currentTouch.view];
if (currentLocation.y > 300) {
_box.position = CGPointMake(_box.position.x, _box.position.y + 20);
NSLog(@"touching");
}
}
@end
我不確定是否要以錯誤的方式進行操作,但是我可以拼命使用一些幫助! 謝謝!
您的代碼中存在許多錯誤,因此更容易向您展示一個示例項目,該項目可以滿足您的要求:
#import "MyScene.h"
@implementation MyScene
{
SKSpriteNode *myNode;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
myNode = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(30, 30)];
myNode.position = CGPointMake(100, 100);
[self addChild:myNode];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if(touchLocation.y > 200)
{
myNode.position = CGPointMake(myNode.position.x, myNode.position.y+10);
}
}
-(void)update:(CFTimeInterval)currentTime
{
//
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.