触摸图像时移动精灵(move sprite when image is touched)

编程入门 行业动态 更新时间:2024-10-24 08:31:33
触摸图像时移动精灵(move sprite when image is touched)

我是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]; }

I'm new to iOS development and would appricate some help. I'm trying to move an image up the screen when ONLY the image is touched.

I've written the following code but when run images moves by touching anywhere on their screen:

- (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]; }

最满意答案

这对我来说现在很有用。 当用户在battleShip SKSpriteNode上向左或向右移动手指时,我移动Node。 希望这会有所帮助。

#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]; } }

This works for me for now. I move Node when user moves finger left or right upon the battleShip SKSpriteNode. Hope this will be helpfull.

#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]; } }

更多推荐

本文发布于:2023-04-28 01:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   精灵   move   touched   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!