touches已在特定移动对象上移动 [英] touchesMoved on a specific moving object

查看:91
本文介绍了touches已在特定移动对象上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spritekit创建适用于iOS 7的游戏.使用此代码,游戏中的圆圈在屏幕上移动了

Im using Spritekit to create a game for iOS 7. The game has circles moving across the screen using this code

_enemy = [SKSpriteNode spriteNodeWithImageNamed:@"greeny"];
        _enemy.position = CGPointMake(CGRectGetMidX(self.frame)+300,
                                      CGRectGetMidY(self.frame));
_enemy.size = CGSizeMake(70, 70);
_enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:35];
_enemy.physicsBody.mass = 0;

_enemy.physicsBody.categoryBitMask = Collisiongreen;
[_enemies addObject:_enemy];
NSLog(@"Enemies%lu",(unsigned long)_enemies.count);

[self addChild:_enemy];
[_enemy runAction:[SKAction moveByX:-900 y:0 duration:4]];
[self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];
[self runAction:[SKAction sequence:@[
                                  [SKAction waitForDuration:1.4],
                                  [SKAction performSelector:@selector(move)
                                                   onTarget:self],
                                     ]]];

我想让用户触摸一个在屏幕上移动的对象时,可以用手指向上或向下移动它(这就是为什么我使用移动的触摸) 我现在设置的代码是

I would like to have it that when the user touches one of the objects moving across the screen that they can move it with their finger either up or down (thats why I'm using touches moved) The code i have set up right now is

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    if(_dead)
        return;
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    if(CGRectContainsPoint(_enemy.frame, location)){

        [_enemy runAction:[SKAction moveTo:[[touches anyObject] locationInNode:self] duration:0.01]];
    }

我的问题是,如果我触摸它,该对象将移动几个像素,但此后将立即停止.我该如何用手指移动它,如果放开它,它将像以前编程的那样继续向左移动?谢谢!!

My problem is that if i touch it the object will move a few pixels but will stop right after. How can I get it to move with my finger and if i let go it will continue moving left like programed before? Thanks!!

推荐答案

所以这里的窍门是,在touchesMoved方法中,您希望将敌人的位置设置为您的触摸位置.使用touchesEnded方法可以执行使敌人朝您最后一次触摸的方向继续前进的方法.这是一个未经测试的粗略示例.

So the trick here is that in the touchesMoved method, you want the enemy's postion set to the location of your touch. Use the touchesEnded method to execute a method that makes the enemy continue in the direction of your last touch. This is a rough non-tested example.

您需要存储当前位置与先前位置的差异.

You need to store the difference of the current location with the previous location.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    if(_dead)
        return;
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    if(CGRectContainsPoint(_enemy.frame, location)){

        someBool = False;
        [_enemy setPosition:location];
        changeInX = location.x - lastX;
        changeInY = location.y - lastY;
        lastX = location.x;
        lastY = location.y;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    someBool = True;
}

- (void)update:(NSTimeInterval)currentTime
{
...
    if (someBool) {
        enemy.position = CGPointMake((enemy.position.x + changeInX), (enemy.position.y + changeInY));
    }
...
}

这篇关于touches已在特定移动对象上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆