【问题标题】:How to make the sprite move positions when the screen is tapped点击屏幕时如何使精灵移动位置
【发布时间】:2014-10-04 16:51:44
【问题描述】:

我希望我的精灵像这样从屏幕的一侧移动到另一侧:

点击前:

http://imgur.com/wIFa3JL

然后在屏幕被点击后:

http://imgur.com/ZtWiE7b

会不会是这样开始的:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

谢谢!

【问题讨论】:

    标签: ios objective-c ios7 sprite-kit


    【解决方案1】:

    我不太确定您是想在触摸屏幕时获取手指的位置并将其应用于精灵的位置,还是只是在触摸屏幕时更改精灵的位置。

    如果您想在 touchesBegan 中获取手指的位置并将该位置应用于精灵:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        for (UITouch *touch in touches) {
            CGPoint location = [touch locationInView:self];
            spriteNode.position = location;
        }
    }
    

    如果您只想在每次触摸时将精灵的位置更改为树的另一侧:

    首先,您需要跟踪您的精灵当前位于哪一侧。让我们创建一个 BOOL 来保存该信息:

    BOOL isRightSide;
    

    然后,在开始时,如果您的精灵从树的右侧开始,只需将 TRUE 分配给布尔值。

    最后,在触摸事件中:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (isRightSide) {
            //Change to sprite's position to the LEFT side 
        } else {
            //Change to sprite's position to the RIGHT side 
        }
    
        isRightSide = !isRightSide;
    }
    

    我希望这会有所帮助 =)

    【讨论】:

    • 当我在应用程序内部单击时它关闭了,你知道为什么吗?感谢您的帮助。
    • 输出框的异常信息是什么?
    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多