{"_id":"547b1cb84366a708001b3b54","__v":27,"user":"54639d06447e0a1000249f7f","category":{"_id":"5463e333447e0a100024a08b","project":"5463e333447e0a100024a087","pages":["5463e334447e0a100024a08d","54646a285871e90800f50174","54667e7af2b6390800dfd82a","54679c8c98b6840800268982","54679d8498b6840800268987","546a2ed0394c861a00c3a413","546b6a4862515a14007ebbba","547b1c2e7eec0f080099e1dd","55d33d90b9ef4435006dec70","56570c764d1c720d001f51bb"],"version":"5463e333447e0a100024a08a","__v":10,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-11-12T22:46:11.951Z","from_sync":false,"order":1,"slug":"documentation","title":"Documentation"},"version":{"_id":"5463e333447e0a100024a08a","project":"5463e333447e0a100024a087","__v":6,"createdAt":"2014-11-12T22:46:11.486Z","releaseDate":"2014-11-12T22:46:11.486Z","categories":["5463e333447e0a100024a08b","54645c365871e90800f50151","547b1c5a4366a708001b3b50","547b1cdf4366a708001b3b5a","54e3bef8464a9c3700f7ca67","54ec651d7ab3172d00c01ab7"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"project":"5463e333447e0a100024a087","updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-11-30T13:33:44.913Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"basic_auth":false,"results":{"codes":[]},"try":true,"auth":"never","params":[],"url":""},"isReference":false,"order":14,"body":"**Process**\n\n  * Create event listener to detect touch\n  * Assign functions for different input states to the event listener\n  * Add the event listener to the event dispatched\n\n##**Single Touch Input** - detect when the user has interacted with their device using touch primarily for mobile devices using a single finger\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// called when the touch first begins\\nbool onTouchBegan( Touch *touch, Event *event );\\n// called when the user moves their finger\\nvoid onTouchMoved( Touch *touch, Event *event );\\n// called when the user lifts their finger\\nvoid onTouchEnded( Touch *touch, Event *event );\\n// called when the device goes to another application such as a phone call\\nvoid onTouchCancelled( Touch *touch, Event *event );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create( );\\nlistener->setSwallowTouches( true );\\n\\nlistener->onTouchBegan = CC_CALLBACK_2( ClassName::onTouchBegan, this );\\nlistener->onTouchMoved = CC_CALLBACK_2( ClassName::onTouchMoved, this );\\nlistener->onTouchEnded = CC_CALLBACK_2( ClassName::onTouchEnded, this );\\nlistener->onTouchCancelled = CC_CALLBACK_2( ClassName::onTouchCancelled, this );\\n\\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\\n\\n// called when the touch first begins\\nbool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)\\n{\\n    CCLOG(\\\"onTouchBegan x = %f, y = %f\\\", touch->getLocation().x, touch->getLocation().y);\\n    \\n    return true; // true if the function wants to swallow the touch\\n}\\n\\n// called when the user moves their finger\\nvoid HelloWorld::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)\\n{\\n    CCLOG(\\\"onTouchMoved x = %f, y = %f\\\", touch->getLocation().x, touch->getLocation().y);\\n}\\n\\n// called when the user lifts their finger\\nvoid HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)\\n{\\n    CCLOG(\\\"onTouchEnded x = %f, y = %f\\\", touch->getLocation().x, touch->getLocation().y);\\n}\\n\\n// called when the device goes to another application such as a phone call\\nvoid HelloWorld::onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *event)\\n{\\n    CCLOG(\\\"onTouchCancelled x = %f, y = %f\\\", touch->getLocation().x, touch->getLocation().y);\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"// checks if the device you are using is capable of touch\\nif ( cc.sys.capabilities.hasOwnProperty( 'touches' ) )\\n{\\n    cc.eventManager.addListener(\\n    {\\n        event: cc.EventListener.TOUCH_ONE_BY_ONE,\\n        \\n      \\t// called when the touch first begins\\n        onTouchBegan:function( touch, event )\\n        {\\n            cc.log( \\\"Touch Began X: \\\" + touch.getLocationX( ) );\\n            cc.log( \\\"Touch Began Y: \\\" + touch.getLocationY( ) );\\n            \\n            return true;\\n        },\\n        \\n      \\t// called when the user moves their finger\\n        onTouchMoved:function( touch, event )\\n        {\\n            cc.log( \\\"Touch Moved X: \\\" + touch.getLocationX( ) );\\n            cc.log( \\\"Touch Moved Y: \\\" + touch.getLocationY( ) );\\n        },\\n        \\n      \\t// called when the user lifts their finger\\n        onTouchEnded:function( touch, event )\\n        {\\n            cc.log( \\\"Touch Ended X: \\\" + touch.getLocationX( ) );\\n            cc.log( \\\"Touch Ended Y: \\\" + touch.getLocationX( ) );\\n        },\\n        \\n      \\t// called when the device goes to another application such as a phone call\\n        onTouchCancelled:function( touch, event )\\n        {\\n            cc.log( \\\"Touch Cancelled X: \\\" + touch.getLocationX( ) );\\n            cc.log( \\\"Touch Cancelled Y: \\\" + touch.getLocationX( ) );\\n        }\\n    }, this );\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote: *Put the JavaScript code inside the constructor*\n\n##**Multi Touch Input** - detect when the user has interacted with their device using touch primarily for mobile devices using a multiple fingers\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// called when the touches first begins\\nvoid onTouchesBegan( const std::vector<Touch *> &touches, cocos2d::Event *event );\\n// called when the user moves their finger(s)\\nvoid onTouchesMoved( const std::vector<Touch *> &touches, cocos2d::Event *event );\\n// called when the user lifts their finger(s)\\nvoid onTouchesEnded( const std::vector<Touch *> &touches, cocos2d::Event *event );\\n// called when the device goes to another application such as a phone call\\nvoid onTouchesCancelled( const std::vector<Touch *> &touches, cocos2d::Event *event );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"EventListenerTouchAllAtOnce *listener = EventListenerTouchAllAtOnce::create( );\\n\\nlistener->onTouchesBegan = CC_CALLBACK_2( ClassName::onTouchesBegan, this );\\nlistener->onTouchesMoved = CC_CALLBACK_2( ClassName::onTouchesMoved, this );\\nlistener->onTouchesEnded = CC_CALLBACK_2( ClassName::onTouchesEnded, this );\\nlistener->onTouchesCancelled = CC_CALLBACK_2( HelloWorld::onTouchesCancelled, this );\\n\\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\\n\\n// called when the touches first begins\\nvoid ClassName::onTouchesBegan( const std::vector<Touch *> &touches, Event *event )\\n{\\n    CCLOG( \\\"Touch Began %f\\\", touches[touchIndex]->getLocation( ).x );\\n}\\n\\n// called when the user moves their finger(s)\\nvoid ClassName::onTouchesMoved( const std::vector<Touch *> &touches, Event *event )\\n{\\n    CCLOG( \\\"Touch Moved %f\\\", touches[touchIndex]->getLocation( ).x );\\n}\\n\\n// called when the user lifts their finger(s)\\nvoid ClassName::onTouchesEnded( const std::vector<Touch *> &touches, Event *event )\\n{\\n    CCLOG( \\\"Touch Ended %f\\\", touches[touchIndex]->getLocation( ).x );\\n}\\n\\n// called when the device goes to another application such as a phone call\\nvoid ClassName::onTouchesCancelled( const std::vector<Touch *> &touches, Event *event )\\n{\\n    CCLOG( \\\"Touch Cancelled %f\\\", touches[touchIndex]->getLocation( ).x );\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"// checks if the device you are using is capable of touch\\nif ( cc.sys.capabilities.hasOwnProperty( 'touches' ) )\\n{\\n    cc.eventManager.addListener(\\n    {\\n        event: cc.EventListener.TOUCH_ALL_AT_ONCE,\\n        \\n        onTouchesBegan:function( touches, event )\\n        {\\n            cc.log( \\\"Touch Began: \\\" + touches[touchIndex].getLocationX( ) );\\n        },\\n        \\n        onTouchesMoved:function( touches, event )\\n        {\\n            cc.log( \\\"Touch Moved: \\\" + touches[touchIndex].getLocationX( ) );\\n        },\\n        \\n        onTouchesEnded:function( touches, event )\\n        {\\n            cc.log( \\\"Touch Ended: \\\" + touches[touchIndex].getLocationX( ) );\\n        },\\n        \\n        onTouchesCancelled:function( touches, event )\\n        {\\n            cc.log( \\\"Touch Cancelled: \\\" + touches[touchIndex].getLocationX( ) );\\n        }\\n    }, this);\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote:* **touchIndex** is the touch you are detecting so 0 would be the first touch in multi touch, 1 would be the second touch in multi touch etc*\n\nNote: *Put the JavaScript code inside the constructor*\n\n##**Mouse Input** - detect when the user has interacted with their device using a mouse\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// when the user presses a mouse button\\nvoid onMouseDown( cocos2d::Event *event );\\n// when the user releases a mouse button\\nvoid onMouseUp( cocos2d::Event *event );\\n// when the mouse is moved\\nvoid onMouseMove( cocos2d::Event *event );\\n// when the mouse wheel is scrolled\\nvoid onMouseScroll( cocos2d::Event *event );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"EventListenerMouse *listener = EventListenerMouse::create( );\\n\\nlistener->onMouseMove = CC_CALLBACK_1( ClassName::onMouseMove, this );\\nlistener->onMouseUp = CC_CALLBACK_1( ClassName::onMouseUp, this );\\nlistener->onMouseDown = CC_CALLBACK_1( ClassName::onMouseDown, this );\\nlistener->onMouseScroll = CC_CALLBACK_1( ClassName::onMouseScroll, this );\\n\\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\\n\\n// when the user presses a mouse button\\nvoid ClassName::onMouseDown( Event *event )\\n{\\n    EventMouse *e = ( EventMouse * )event;\\n\\n    e->getMouseButton( );\\n    e->getCursorX( );\\n    e->getCursorY( );\\n}\\n\\n// when the user releases a mouse button\\nvoid ClassName::onMouseUp( Event *event )\\n{\\n    \\n}\\n\\n// when the mouse is moved\\nvoid ClassName::onMouseMove( Event *event )\\n{\\n    \\n}\\n\\n// when the mouse wheel is scrolled\\nvoid ClassName::onMouseScroll( Event *event )\\n{\\n    EventMouse *e = ( EventMouse * )event;\\n    e->getScrollX( );\\n    e->getScrollY( );\\n    e->getDelta();\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"// checks if the device you are using is capable of mouse input\\nif ( cc.sys.capabilities.hasOwnProperty( 'mouse' ) )\\n{\\n    cc.eventManager.addListener(\\n    {\\n        event: cc.EventListener.MOUSE,\\n        \\n        onMouseDown: function( event )\\n        {\\n            if ( event.getButton( ) == cc.EventMouse.BUTTON_LEFT )\\n            {\\n                cc.log( \\\"Left mouse button pressed at \\\" + event.getLocationX( ) );\\n            }\\n        },\\n        \\n        onMouseUp: function(event)\\n        {\\n            if ( event.getButton() == cc.EventMouse.BUTTON_LEFT )\\n            {\\n                cc.log( \\\"Left mouse button released at \\\" + event.getLocationX( ) );\\n            }\\n        },\\n        \\n        onMouseMove: function(event)\\n        {\\n            cc.log( \\\"Mouse Moved: \\\" + event.getLocationX( ) );\\n        },\\n        \\n        onMouseScroll: function(event)\\n        {\\n            cc.log( \\\"Scroll: \\\" + event.getLocationX( ) );\\n        }\\n    }, this );\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote: *Put the JavaScript code inside the constructor*\n\n##**Keyboard Input** - detect when the user has interacted with their device using a keyboard\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// called when a key is pressed on the keyboard\\nvoid onKeyPressed( EventKeyboard::KeyCode keyCode, Event *event );\\n// called when a key is released on the keyboard\\nvoid onKeyReleased( EventKeyboard::KeyCode keyCode, Event *event );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"EventListenerKeyboard *listener = EventListenerKeyboard::create( );\\n\\nlistener->onKeyPressed = CC_CALLBACK_2( ClassName::onKeyPressed, this );\\nlistener->onKeyReleased = CC_CALLBACK_2( ClassName::onKeyReleased, this );\\n\\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\\n\\n// called when a key is pressed on the keyboard\\nvoid ClassName::onKeyPressed( EventKeyboard::KeyCode keyCode, Event *event )\\n{\\n    CCLOG( \\\"Key with keycode %d pressed\\\", keyCode );\\n}\\n\\n// called when a key is released on the keyboard\\nvoid ClassName::onKeyReleased( EventKeyboard::KeyCode keyCode, Event *event )\\n{\\n    CCLOG( \\\"Key with keycode %d released\\\", keyCode );\\n}\\n\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"// checks if the device you are using is capable of keyboard input\\nif ( cc.sys.capabilities.hasOwnProperty( 'keyboard' ) )\\n{\\n    cc.eventManager.addListener(\\n    {\\n        event: cc.EventListener.KEYBOARD,\\n        \\n        onKeyPressed: function( key, event )\\n        {\\n            cc.log( \\\"Key Pressed: \\\" + key.toString( ) );\\n        },\\n        \\n        onKeyReleased: function( key, event )\\n        {\\n            cc.log( \\\"Key Released: \\\" + key.toString( ) );\\n        }\\n    }, this );\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote: *Put the JavaScript code inside the constructor*\n\n##**Accelerometer Input** - detect when the user has interacted with their device by tilting it\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// called when the device is tilted\\nvoid OnAcceleration( Acceleration *acc, Event *event );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"Device::setAccelerometerEnabled( true );\\n\\nEventListenerAcceleration *listener = EventListenerAcceleration::create( CC_CALLBACK_2( ClassName::OnAcceleration, this ) );\\n\\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\\n\\n// called when the device is tilted\\nvoid ClassName::OnAcceleration( Acceleration *acc, Event *event )\\n{\\n    CCLOG( \\\"X axis tilt %f\\\", acc->x );\\n    CCLOG( \\\"Y axis tilt %f\\\", acc->y );\\n    CCLOG( \\\"Z axis tilt %f\\\", acc->z );\\n}\\n\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"// checks if the device you are using is capable of accelerometer input\\nif ( cc.sys.capabilities.hasOwnProperty( 'accelerometer' ) )\\n{\\n    cc.inputManager.setAccelerometerInterval( 1 / 60 );\\n    cc.inputManager.setAccelerometerEnabled( true );\\n    \\n    cc.eventManager.addListener(\\n    {\\n        event: cc.EventListener.ACCELERATION,\\n        \\n        callback: function( accelEvent, event )\\n        {\\n            cc.log( 'Accel X: ' + accelEvent.x + ' Y: ' + accelEvent.y + ' Z: ' + accelEv\\n        }\\n    }, this );\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote: *Put the JavaScript code inside the constructor*","excerpt":"This page will cover the different ways that the user can interact with the application that Cocos provides.","slug":"input","type":"basic","title":"Input"}

Input

This page will cover the different ways that the user can interact with the application that Cocos provides.

**Process** * Create event listener to detect touch * Assign functions for different input states to the event listener * Add the event listener to the event dispatched ##**Single Touch Input** - detect when the user has interacted with their device using touch primarily for mobile devices using a single finger [block:code] { "codes": [ { "code": "// called when the touch first begins\nbool onTouchBegan( Touch *touch, Event *event );\n// called when the user moves their finger\nvoid onTouchMoved( Touch *touch, Event *event );\n// called when the user lifts their finger\nvoid onTouchEnded( Touch *touch, Event *event );\n// called when the device goes to another application such as a phone call\nvoid onTouchCancelled( Touch *touch, Event *event );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create( );\nlistener->setSwallowTouches( true );\n\nlistener->onTouchBegan = CC_CALLBACK_2( ClassName::onTouchBegan, this );\nlistener->onTouchMoved = CC_CALLBACK_2( ClassName::onTouchMoved, this );\nlistener->onTouchEnded = CC_CALLBACK_2( ClassName::onTouchEnded, this );\nlistener->onTouchCancelled = CC_CALLBACK_2( ClassName::onTouchCancelled, this );\n\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\n\n// called when the touch first begins\nbool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)\n{\n CCLOG(\"onTouchBegan x = %f, y = %f\", touch->getLocation().x, touch->getLocation().y);\n \n return true; // true if the function wants to swallow the touch\n}\n\n// called when the user moves their finger\nvoid HelloWorld::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)\n{\n CCLOG(\"onTouchMoved x = %f, y = %f\", touch->getLocation().x, touch->getLocation().y);\n}\n\n// called when the user lifts their finger\nvoid HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)\n{\n CCLOG(\"onTouchEnded x = %f, y = %f\", touch->getLocation().x, touch->getLocation().y);\n}\n\n// called when the device goes to another application such as a phone call\nvoid HelloWorld::onTouchCancelled(cocos2d::Touch *touch, cocos2d::Event *event)\n{\n CCLOG(\"onTouchCancelled x = %f, y = %f\", touch->getLocation().x, touch->getLocation().y);\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "// checks if the device you are using is capable of touch\nif ( cc.sys.capabilities.hasOwnProperty( 'touches' ) )\n{\n cc.eventManager.addListener(\n {\n event: cc.EventListener.TOUCH_ONE_BY_ONE,\n \n \t// called when the touch first begins\n onTouchBegan:function( touch, event )\n {\n cc.log( \"Touch Began X: \" + touch.getLocationX( ) );\n cc.log( \"Touch Began Y: \" + touch.getLocationY( ) );\n \n return true;\n },\n \n \t// called when the user moves their finger\n onTouchMoved:function( touch, event )\n {\n cc.log( \"Touch Moved X: \" + touch.getLocationX( ) );\n cc.log( \"Touch Moved Y: \" + touch.getLocationY( ) );\n },\n \n \t// called when the user lifts their finger\n onTouchEnded:function( touch, event )\n {\n cc.log( \"Touch Ended X: \" + touch.getLocationX( ) );\n cc.log( \"Touch Ended Y: \" + touch.getLocationX( ) );\n },\n \n \t// called when the device goes to another application such as a phone call\n onTouchCancelled:function( touch, event )\n {\n cc.log( \"Touch Cancelled X: \" + touch.getLocationX( ) );\n cc.log( \"Touch Cancelled Y: \" + touch.getLocationX( ) );\n }\n }, this );\n}", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note: *Put the JavaScript code inside the constructor* ##**Multi Touch Input** - detect when the user has interacted with their device using touch primarily for mobile devices using a multiple fingers [block:code] { "codes": [ { "code": "// called when the touches first begins\nvoid onTouchesBegan( const std::vector<Touch *> &touches, cocos2d::Event *event );\n// called when the user moves their finger(s)\nvoid onTouchesMoved( const std::vector<Touch *> &touches, cocos2d::Event *event );\n// called when the user lifts their finger(s)\nvoid onTouchesEnded( const std::vector<Touch *> &touches, cocos2d::Event *event );\n// called when the device goes to another application such as a phone call\nvoid onTouchesCancelled( const std::vector<Touch *> &touches, cocos2d::Event *event );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "EventListenerTouchAllAtOnce *listener = EventListenerTouchAllAtOnce::create( );\n\nlistener->onTouchesBegan = CC_CALLBACK_2( ClassName::onTouchesBegan, this );\nlistener->onTouchesMoved = CC_CALLBACK_2( ClassName::onTouchesMoved, this );\nlistener->onTouchesEnded = CC_CALLBACK_2( ClassName::onTouchesEnded, this );\nlistener->onTouchesCancelled = CC_CALLBACK_2( HelloWorld::onTouchesCancelled, this );\n\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\n\n// called when the touches first begins\nvoid ClassName::onTouchesBegan( const std::vector<Touch *> &touches, Event *event )\n{\n CCLOG( \"Touch Began %f\", touches[touchIndex]->getLocation( ).x );\n}\n\n// called when the user moves their finger(s)\nvoid ClassName::onTouchesMoved( const std::vector<Touch *> &touches, Event *event )\n{\n CCLOG( \"Touch Moved %f\", touches[touchIndex]->getLocation( ).x );\n}\n\n// called when the user lifts their finger(s)\nvoid ClassName::onTouchesEnded( const std::vector<Touch *> &touches, Event *event )\n{\n CCLOG( \"Touch Ended %f\", touches[touchIndex]->getLocation( ).x );\n}\n\n// called when the device goes to another application such as a phone call\nvoid ClassName::onTouchesCancelled( const std::vector<Touch *> &touches, Event *event )\n{\n CCLOG( \"Touch Cancelled %f\", touches[touchIndex]->getLocation( ).x );\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "// checks if the device you are using is capable of touch\nif ( cc.sys.capabilities.hasOwnProperty( 'touches' ) )\n{\n cc.eventManager.addListener(\n {\n event: cc.EventListener.TOUCH_ALL_AT_ONCE,\n \n onTouchesBegan:function( touches, event )\n {\n cc.log( \"Touch Began: \" + touches[touchIndex].getLocationX( ) );\n },\n \n onTouchesMoved:function( touches, event )\n {\n cc.log( \"Touch Moved: \" + touches[touchIndex].getLocationX( ) );\n },\n \n onTouchesEnded:function( touches, event )\n {\n cc.log( \"Touch Ended: \" + touches[touchIndex].getLocationX( ) );\n },\n \n onTouchesCancelled:function( touches, event )\n {\n cc.log( \"Touch Cancelled: \" + touches[touchIndex].getLocationX( ) );\n }\n }, this);\n}", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note:* **touchIndex** is the touch you are detecting so 0 would be the first touch in multi touch, 1 would be the second touch in multi touch etc* Note: *Put the JavaScript code inside the constructor* ##**Mouse Input** - detect when the user has interacted with their device using a mouse [block:code] { "codes": [ { "code": "// when the user presses a mouse button\nvoid onMouseDown( cocos2d::Event *event );\n// when the user releases a mouse button\nvoid onMouseUp( cocos2d::Event *event );\n// when the mouse is moved\nvoid onMouseMove( cocos2d::Event *event );\n// when the mouse wheel is scrolled\nvoid onMouseScroll( cocos2d::Event *event );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "EventListenerMouse *listener = EventListenerMouse::create( );\n\nlistener->onMouseMove = CC_CALLBACK_1( ClassName::onMouseMove, this );\nlistener->onMouseUp = CC_CALLBACK_1( ClassName::onMouseUp, this );\nlistener->onMouseDown = CC_CALLBACK_1( ClassName::onMouseDown, this );\nlistener->onMouseScroll = CC_CALLBACK_1( ClassName::onMouseScroll, this );\n\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\n\n// when the user presses a mouse button\nvoid ClassName::onMouseDown( Event *event )\n{\n EventMouse *e = ( EventMouse * )event;\n\n e->getMouseButton( );\n e->getCursorX( );\n e->getCursorY( );\n}\n\n// when the user releases a mouse button\nvoid ClassName::onMouseUp( Event *event )\n{\n \n}\n\n// when the mouse is moved\nvoid ClassName::onMouseMove( Event *event )\n{\n \n}\n\n// when the mouse wheel is scrolled\nvoid ClassName::onMouseScroll( Event *event )\n{\n EventMouse *e = ( EventMouse * )event;\n e->getScrollX( );\n e->getScrollY( );\n e->getDelta();\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "// checks if the device you are using is capable of mouse input\nif ( cc.sys.capabilities.hasOwnProperty( 'mouse' ) )\n{\n cc.eventManager.addListener(\n {\n event: cc.EventListener.MOUSE,\n \n onMouseDown: function( event )\n {\n if ( event.getButton( ) == cc.EventMouse.BUTTON_LEFT )\n {\n cc.log( \"Left mouse button pressed at \" + event.getLocationX( ) );\n }\n },\n \n onMouseUp: function(event)\n {\n if ( event.getButton() == cc.EventMouse.BUTTON_LEFT )\n {\n cc.log( \"Left mouse button released at \" + event.getLocationX( ) );\n }\n },\n \n onMouseMove: function(event)\n {\n cc.log( \"Mouse Moved: \" + event.getLocationX( ) );\n },\n \n onMouseScroll: function(event)\n {\n cc.log( \"Scroll: \" + event.getLocationX( ) );\n }\n }, this );\n}", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note: *Put the JavaScript code inside the constructor* ##**Keyboard Input** - detect when the user has interacted with their device using a keyboard [block:code] { "codes": [ { "code": "// called when a key is pressed on the keyboard\nvoid onKeyPressed( EventKeyboard::KeyCode keyCode, Event *event );\n// called when a key is released on the keyboard\nvoid onKeyReleased( EventKeyboard::KeyCode keyCode, Event *event );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "EventListenerKeyboard *listener = EventListenerKeyboard::create( );\n\nlistener->onKeyPressed = CC_CALLBACK_2( ClassName::onKeyPressed, this );\nlistener->onKeyReleased = CC_CALLBACK_2( ClassName::onKeyReleased, this );\n\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\n\n// called when a key is pressed on the keyboard\nvoid ClassName::onKeyPressed( EventKeyboard::KeyCode keyCode, Event *event )\n{\n CCLOG( \"Key with keycode %d pressed\", keyCode );\n}\n\n// called when a key is released on the keyboard\nvoid ClassName::onKeyReleased( EventKeyboard::KeyCode keyCode, Event *event )\n{\n CCLOG( \"Key with keycode %d released\", keyCode );\n}\n", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "// checks if the device you are using is capable of keyboard input\nif ( cc.sys.capabilities.hasOwnProperty( 'keyboard' ) )\n{\n cc.eventManager.addListener(\n {\n event: cc.EventListener.KEYBOARD,\n \n onKeyPressed: function( key, event )\n {\n cc.log( \"Key Pressed: \" + key.toString( ) );\n },\n \n onKeyReleased: function( key, event )\n {\n cc.log( \"Key Released: \" + key.toString( ) );\n }\n }, this );\n}", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note: *Put the JavaScript code inside the constructor* ##**Accelerometer Input** - detect when the user has interacted with their device by tilting it [block:code] { "codes": [ { "code": "// called when the device is tilted\nvoid OnAcceleration( Acceleration *acc, Event *event );", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "Device::setAccelerometerEnabled( true );\n\nEventListenerAcceleration *listener = EventListenerAcceleration::create( CC_CALLBACK_2( ClassName::OnAcceleration, this ) );\n\nDirector::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );\n\n// called when the device is tilted\nvoid ClassName::OnAcceleration( Acceleration *acc, Event *event )\n{\n CCLOG( \"X axis tilt %f\", acc->x );\n CCLOG( \"Y axis tilt %f\", acc->y );\n CCLOG( \"Z axis tilt %f\", acc->z );\n}\n", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "// checks if the device you are using is capable of accelerometer input\nif ( cc.sys.capabilities.hasOwnProperty( 'accelerometer' ) )\n{\n cc.inputManager.setAccelerometerInterval( 1 / 60 );\n cc.inputManager.setAccelerometerEnabled( true );\n \n cc.eventManager.addListener(\n {\n event: cc.EventListener.ACCELERATION,\n \n callback: function( accelEvent, event )\n {\n cc.log( 'Accel X: ' + accelEvent.x + ' Y: ' + accelEvent.y + ' Z: ' + accelEv\n }\n }, this );\n}", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note: *Put the JavaScript code inside the constructor*