首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按住按钮发射射弹

按住按钮发射射弹
EN

Stack Overflow用户
提问于 2015-02-02 14:03:29
回答 1查看 347关注 0票数 0

在我的游戏中,我想要做的是在保持射击按钮的同时,不断地发射弹丸。我一直使用的是一个简单的touchesBegan和touchesMoved (也就是touchesBegan)解决方案。因此,我遇到的问题是:只有在按住按钮时才射击一次,但如果在按住按钮时移动触点(它调用touchesMoved方法),则一次只能发射多个弹丸。

我需要帮助的是:如何在按住按钮的同时不断地射击这些弹丸,就像你在触摸向下的方法中所做的那样?希望我所要求的是可能的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-02 14:22:01

您可以使用touchesBegantouchesEnded函数来跟踪按下按钮的时间。然后利用update函数对SKScene进行延迟发射。

逻辑是在按下按钮时将布尔shooting设置为true。shootingtouchesEnded中被设置为false。这样我们就能跟踪触觉。然后,在update函数中,如果shooting变量为真,则发射弹丸。

在目标C中

代码语言:javascript
复制
//GameScene.h

@property (nonatomic,strong) SKSpriteNode *shootButton;

//GameScene.m

BOOL shooting = false;
CFTimeInterval lastShootingTime = 0;
CFTimeInterval delayBetweenShots = 0.5;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    if  ([self nodeAtPoint:location] == self.shootButton)
    {
        shooting = true;
        NSLog(@"start shooting");
    }

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    if  ([self nodeAtPoint:location] == self.shootButton)
    {
        shooting = false;
        NSLog(@"stop shooting");
    }
}


-(void)shoot
{
    // Projectile code
    NSLog(@"shooting");
}

-(void)update:(NSTimeInterval)currentTime {

    if (shooting)
    {
        NSTimeInterval delay = currentTime - lastShootingTime;
        if (delay >= delayBetweenShots) {
            [self shoot];
            lastShootingTime = currentTime;
        }
    }
}

斯威夫特

代码语言:javascript
复制
var shootButton : SKSpriteNode!

var shooting = false
var lastShootingTime : CFTimeInterval = 0
var delayBetweenShots : CFTimeInterval = 0.5

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch: AnyObject? = touches.anyObject()
    if let location = touch?.locationInNode(self)
    {
        if self.nodeAtPoint(location) == shootButton
        {
            self.shooting = true
            println("start shooting")
        }
    }
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    let touch: AnyObject? = touches.anyObject()
    if let location = touch?.locationInNode(self)
    {
        if self.nodeAtPoint(location) == shootButton
        {
            self.shooting = false
            println("stop shooting")
        }
    }
}

func shoot()
{
    // Projectile code
    println("shooting")
}

override func update(currentTime: NSTimeInterval) {

    if shooting
    {
        let delay = currentTime - lastShootingTime
        if delay >= delayBetweenShots {
            shoot()
            lastShootingTime = currentTime
        }
    }
}

您可以调整delayBetweenShots变量以更改触发所需的速度。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28279471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档