Cocos2dx touch piano
1. Touch Piano Project Description 1.1 touch piano Function Description
You can press your finger to press the piano key to send a corresponding tone, press the position to display the particle effect of the nebula, scroll to the position of another piano key, and press the android return key to close it.
1.2 techniques required for piano touch
Particle effects, Android button listening.
2. Touch the piano detailed design 2.1 click the piano key to send the corresponding tone
First, create the UI, and the bottom layer is a scroll layer. Add the corresponding piano key buttons according to the position above. Set the Tag for each piano key button and set the name according to the tone. Note: The black key button must be adjusted to the white key through the object structure tree of cocostudio. In this way, the black key is preferentially responded when you click the black key.
The Tag value is used to traverse each button control. If a corresponding button object exists, the response is bound.
or (int i = 0; i < 70; i++){auto btn = (Button*)Helper::seekWidgetByTag(UI, i);if (btn){btn->addTouchEventListener(this, toucheventselector(CPaino::onBtnTouch));}}
In onBtnTouch, the most clicked Object Ref is passed into the function. Convert the input object type to obtain the name, and then spell out the path name of the sound file to play the sound file.
/*************************************** * ************** Function: touch button response input parameter: Ref * target: TouchEventType type: click type ************************************* * *****************/void CPaino:: onBtnTouch (Ref * target, TouchEventType) {auto btn = (Button *) target; if (btn & type = TouchEventType: TOUCH_EVENT_BEGAN) {std :: string sBtnName = btn-> getName (); std: string sSoundName = StringUtils: format (sound/audio s.wav, sBtnName. c_str (); CocosDenshion: SimpleAudioEngine: getInstance ()-> playEffect (sSoundName. c_str ());}}2.2 generate yunxing particle effects at the touch position
Use the particle effect in cocos2dx. Note that you need to mount a touch layer on the current layer of the piano key. Play the particle effect at the touch point.
2.3 listen to Android buttons
Added the function that allows receiving key events during initialization.
auto listenerKeyPad = EventListenerKeyboard::create();listenerKeyPad->onKeyReleased = CC_CALLBACK_2(CPaino::onKeyPadReleased,this);_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerKeyPad,this);
Then write onKeyPadReleased to identify the return key to exit the game.
4. Touch the piano running result