在我的游戏中,我想要做的是在保持射击按钮的同时,不断地发射弹丸。我一直使用的是一个简单的touchesBegan和touchesMoved (也就是touchesBegan)解决方案。因此,我遇到的问题是:只有在按住按钮时才射击一次,但如果在按住按钮时移动触点(它调用touchesMoved方法),则一次只能发射多个弹丸。
我需要帮助的是:如何在按住按钮的同时不断地射击这些弹丸,就像你在触摸向下的方法中所做的那样?希望我所要求的是可能的。
发布于 2015-02-02 14:22:01
您可以使用touchesBegan和touchesEnded函数来跟踪按下按钮的时间。然后利用update函数对SKScene进行延迟发射。
逻辑是在按下按钮时将布尔shooting设置为true。shooting在touchesEnded中被设置为false。这样我们就能跟踪触觉。然后,在update函数中,如果shooting变量为真,则发射弹丸。
在目标C中
//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;
}
}
}斯威夫特
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变量以更改触发所需的速度。
https://stackoverflow.com/questions/28279471
复制相似问题