touchesBegan
및 touchesEnded
기능을 사용하여 버튼을 누른 시간을 추적 할 수 있습니다. 그런 다음 SKScene
의 update
기능을 사용하여 지연된 발사체를 발사하십시오.
논리는 버튼을 누를 때 부울 shooting
을 true로 설정하는 것입니다. shooting
은 touchesEnded
안에 false로 설정됩니다. 이렇게하면 접촉을 추적 할 수 있습니다. 그런 다음 update
함수에서 shooting
변수가 true 인 경우 투영 물이 촬영됩니다. 스위프트
에서 목표 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
변수를 조정할 수 있습니다.
우리가 그것을 도울 수 있도록 현재 구현을 게시하십시오 – LearnCocos2D
[연속적인 이벤트 발사를 구현하고 유지하기위한 우아한 방법?] (http://stackoverflow.com/questions/13248889/elegant-way-to- 반복적 인 이벤트 발사) –