按住按钮时发射弹丸
shoot projectiles while button is held
我想在我的游戏中做的是在按住射击按钮的同时不断发射射弹。我一直在使用的是简单的 touchesBegan 和 touchesMoved(仅调用 touchesBegan)解决方案。所以我遇到的问题是:如果你按住按钮你只会发射一次,但是如果你在按住它的同时稍微移动触摸点(调用touchesMoved方法),你将一次发射多个弹丸。
我需要帮助的是:如何在按住按钮的同时不断发射这些射弹,就像您在 Touch Down 类方法中所做的那样?希望我所要求的是可能的。
您可以使用 touchesBegan
和 touchesEnded
函数来跟踪按下按钮的时间。然后使用SKScene
的update
功能,延迟发射弹丸。
逻辑是在按下按钮时将布尔值 shooting
设置为 true。 shooting
在 touchesEnded
中设置为 false。这样我们就可以跟踪触摸。然后在 update
函数中,如果 shooting
变量为真,则发射弹丸。
在Objective 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;
}
}
}
在Swift
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
变量来改变触发的速度。
我想在我的游戏中做的是在按住射击按钮的同时不断发射射弹。我一直在使用的是简单的 touchesBegan 和 touchesMoved(仅调用 touchesBegan)解决方案。所以我遇到的问题是:如果你按住按钮你只会发射一次,但是如果你在按住它的同时稍微移动触摸点(调用touchesMoved方法),你将一次发射多个弹丸。
我需要帮助的是:如何在按住按钮的同时不断发射这些射弹,就像您在 Touch Down 类方法中所做的那样?希望我所要求的是可能的。
您可以使用 touchesBegan
和 touchesEnded
函数来跟踪按下按钮的时间。然后使用SKScene
的update
功能,延迟发射弹丸。
逻辑是在按下按钮时将布尔值 shooting
设置为 true。 shooting
在 touchesEnded
中设置为 false。这样我们就可以跟踪触摸。然后在 update
函数中,如果 shooting
变量为真,则发射弹丸。
在Objective 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;
}
}
}
在Swift
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
变量来改变触发的速度。