0
我想知道一旦MainPlayer接觸星星後如何消失某些精靈(星星)。如何讓某些精靈在觸摸後消失 - Cocos2d
謝謝。順便說一句,我是Cocos2d的新手,我只是爲了娛樂和教育目的而做的。 謝謝。
我想知道一旦MainPlayer接觸星星後如何消失某些精靈(星星)。如何讓某些精靈在觸摸後消失 - Cocos2d
謝謝。順便說一句,我是Cocos2d的新手,我只是爲了娛樂和教育目的而做的。 謝謝。
如果您希望能夠檢測到cocos2d中的觸摸,則需要在init方法中將isTouchEnabled
屬性設置爲YES
。您現在可以利用觸摸事件。
接下來,創建新的方法:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//This method gets triggered every time a tap is detected
NSSet *allTouches = [event allTouches]; //Get all the current touches in the event
UITouch *aTouch = [allTouches anyObject]; //Get one of the touches, multitouch is disabled, so there is only always going to be one.
CGPoint pos = [aTouch locationInView:touch.view]; //Get the location of the touch
CGPoint ccPos = [[CCDirector sharedDirector] convertToGL:pos]; //Convert that location to something cocos2d can use
if (CGRectContainsPoint(yourSprite.boundingBox, ccPos)) //Method to check if a rectangle contains a point
{
yourSprite.visible = NO; //Make your sprite invisible
}
}
,你可能要採取最終有以下優勢其他方法:
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
希望這有助於。