Cocos2dx touch piano

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.