2015-02-02 3 views
0

내 게임에서 내가하고 싶은 것은 발사 버튼을 누르고있는 동안 발사체가 계속 발사되도록하는 것이다. 내가 사용한 것은 간단한 touchesBegan과 touchesMoved (단지 touchesBegan을 호출하는) 솔루션입니다. 그래서 내가 직면 한 문제는 : 버튼을 누르고 있으면 한 번만 쏘지 만, 터치 포인트 (touchesMoved 메서드를 호출하는)를 잡고 있으면 조금씩 움직이면 한 번에 여러 개의 발사체를 쏘게됩니다.버튼을 누르고있는 동안 발사체를 쏜다.

도움이 필요한 것 : 터치 다운 방식과 마찬가지로 버튼을 누르고있는 동안 지속적으로이 발사체를 계속 촬영하는 방법은 무엇입니까? 희망 한 것은 가능합니다.

+0

우리가 그것을 도울 수 있도록 현재 구현을 게시하십시오 – LearnCocos2D

+0

[연속적인 이벤트 발사를 구현하고 유지하기위한 우아한 방법?] (http://stackoverflow.com/questions/13248889/elegant-way-to- 반복적 인 이벤트 발사) –

답변

0

touchesBegantouchesEnded 기능을 사용하여 버튼을 누른 시간을 추적 할 수 있습니다. 그런 다음 SKSceneupdate 기능을 사용하여 지연된 발사체를 발사하십시오.

논리는 버튼을 누를 때 부울 shooting을 true로 설정하는 것입니다. shootingtouchesEnded 안에 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 변수를 조정할 수 있습니다.

+0

나는이 스위프트 코드에 대해 거의 알지 못하지만 Objective-C에서 구현했지만 어느 정도 이해하지만, 옆에있는 버튼을 QUICKLY TAPPING하여 무시하는 것으로 나타났습니다. 그것. 이것은 내가 원하는 것이 아닙니다. 어떻게 그걸 검사 할 수 있습니까? – TheDood

+0

touchesBegan에서 lastshootingtime = -1을 제거해보십시오. – rakeshbs

+0

감사합니다. 그것이 한 줄의 코드를 만드는 것이 얼마나 어려운지 ... – TheDood

관련 문제