Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 触摸图像时移动精灵_Ios_Sprite Kit - Fatal编程技术网

Ios 触摸图像时移动精灵

Ios 触摸图像时移动精灵,ios,sprite-kit,Ios,Sprite Kit,我是iOS开发新手,希望能得到一些帮助。我试图在只触摸图像时将图像向上移动 我已经编写了以下代码,但当运行时,图像通过触摸屏幕上的任何位置移动: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* create spritenode and add rocket image */ SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithImag

我是iOS开发新手,希望能得到一些帮助。我试图在只触摸图像时将图像向上移动

我已经编写了以下代码,但当运行时,图像通过触摸屏幕上的任何位置移动:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    /* create spritenode and add rocket image */

    SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithImageNamed:@"rocket.png"];    
    sprite1.position = CGPointMake(400,400);

    SKAction *liftoff = [SKAction moveByX:0 y:1000 duration: 2];
    [sprite1 runAction:liftoff];

    [self addChild:sprite1];
}

更改您的方法,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  if ([touch view] == ImageViewOfYourImage) {
    /* create spritenode and add rocket image */

    SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithImageNamed:@"rocket.png"];
    sprite1.position = CGPointMake(400,400);

    SKAction *liftoff = [SKAction moveByX:0 y:1000 duration: 2];
    [sprite1 runAction:liftoff];

    [self addChild:sprite1];
  }
}

其中,
ImageViewOfYourImage
是指显示图像的视图。或者您可以使用来检查对象的其他属性。

这对我来说暂时有效。当用户在战列舰SKSpriteNode上向左或向右移动手指时,我移动节点。
希望这将是有益的

#import "NewGame.h"

@implementation NewGame

-(id)initWithSize:(CGSize)size
{
    if (self == [super initWithSize:size])

   {
        self.backgroundColor = [SKColor blackColor];

        SKSpriteNode *battleShip = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
        CGRect battleShipFrame = CGRectMake(10, 10, 100, 100);
        [battleShip setSize:battleShipFrame.size];
        battleShip.position = CGPointMake(CGRectGetMidX(self.frame), 50);

        [self addChild:battleShip];
        _ship = battleShip; //@property (nonatomic, strong) SKSpriteNode *ship; in header file
    }
    return self;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint currentLocation = [[touches anyObject] locationInNode:self];
    CGPoint previousLocation = [[touches anyObject] previousLocationInNode:self];
    CGRect shipRect = _ship.frame;
    if (CGRectContainsPoint(shipRect, previousLocation))
    {
        CGPoint lvPosition = CGPointMake(_ship.position.x - (previousLocation.x - currentLocation.x), _ship.position.y);
        _ship.position = lvPosition; // works for me from Apple`s demo project

    //tried to use the under code - but it simpy moves spaceship away from screen)
    // so enden with simple setting the " position " property of SkSpriteNode
    // _ship   :

    //SKAction *moveNode = [SKAction moveByX:lvPosition.x y:0.0 duration:0.0];
    //[_ship runAction: moveNode];
    }
}

您编写的代码在哪里,即您在哪个类中重写了此方法?@hatfinch我假设他当前的UIViewController,因为他没有指定任何其他内容。我认为它一定是SKNode的子类,因为这是唯一响应
-addChild:
的类。但我认为无论是谁回答这个问题(不是我,我不知道SpriteKit)都必须有一个更广泛的背景。这可能不是你想要做的-每次触摸都会创建一个新的火箭精灵,而不是像他想要的那样移动旧的。@Matt问题:图像通过触摸任何地方而移动。我的建议仅通过匹配目标图像的条件来限制代码执行。怎么了?是的,没错,我想在点击后移动“火箭”精灵,而不是创建新精灵。@PaulGuymer在触摸“火箭”之前存在吗?你在哪里创建它?您在哪里存储对它的引用?谢谢,我使用了touchMoved事件,它现在似乎工作正常。我很高兴能为您提供帮助)