In this tutorial we are going to look at adding keyboard and mouse support to your cocos2D application. cocos2D was traditionally used on the iPhone, so keyboard and mouse support is very touch centric, so if you are used to desktop development and things seem a bit strange, this is why.
Alright, let’s jump right in. We are going to create a simple game with our jet sprite from tutorial 3. The big difference is it will respond to your mouse and keyboard activity. Clone your previous project, this time I am calling it MyThirdApp. Yeah, tons of thought went into these names!
Now create a new file in the Classes directory named JetSprite.js. Inside, write the following code:
var JetSprite = cc.Sprite.extend({ _currentRotation:0, ctor:function(){ this.initWithFile("images/jet.png"); }, update:function(dt){ this.setRotation(this._currentRotation); }, handleKey:function(e) { if(e === cc.KEY.left) { this._currentRotation--; } else if(e === cc.KEY.right) this._currentRotation++; if(this._currentRotation < 0) this._currentRotation = 360; if(this._currentRotation > 360) this._currentRotation = 0; }, handleTouch:function(touchLocation) { if(touchLocation.x < 300) this._currentRotation = 0; else this._currentRotation = 180; }, handleTouchMove:function(touchLocation){ // Gross use of hardcoded width,height params. var angle = Math.atan2(touchLocation.x-300,touchLocation.y-300); angle = angle * (180/Math.PI); this._currentRotation = angle; } });
Here we are deriving our new class JetSprite from cc.Sprite using the extend() function. During the constructor, we first call back to cc.Sprite’s constructor, then load our sprite from the file jet.png located in the images folder. ( If you like me started your project by cloning MySecondApp, it should already be there, otherwise make sure you have an image there ).
The update() function is going to be called once every frame, this will make sense later. Essentially this is where you put your object’s logic. In this case we are simply rotating our sprite to the value in _currentRotation. handleKey(), handleTouch() and handleTouchMove() are three user defined functions that are going to be used to pass I/O events into our JetSprite object.
handleKey() is passed the key value ( it’s a number ) of the currently pressed key. cocos2D defines a number of key values to represent each key in the cc.KEY value. If you want more details check out CCKeyboardDispatcher.js in the cocos2D directory for more details. In the event the left arrow key is held down, we decrement our rotation value, if the right arrow key is down we increment the rotation value. Finally, if we roll over 360 or under 0 degrees, we update accordingly. Effectively this rotates our sprite left and right on arrow key press.
handleTouch() is very simple. It is passed in the touchLocation, which is an x,y value representing where on screen was touched ( or in our case, clicked ). If its on the left half the screen, we point up ( 0 degrees ), if the touch occurred on the right half of the screen, we rotate to straight down ( 180 degrees ). One limitation of cocos2D seems to be the lack of ability to handle right clicks, but this may just be an oversight on my behalf. Right now, a “touch” is any mouse button click.
handleTouchMove() is passed in the x,y coordinates of the mouse’s currently location. As you can tell by the comment, I rather crudely used a hardcoded value for representing half the screen width ( 300 ), obviously in non-demo code you wouldn’t do this! What we want to do here is figure out the angle of the mouse relative to the center of our sprite ( which is located at 300,300 ). This is done by taking the atan2 of the distance of the mouse’s coordinate from sprite’s center. We then convert that value ( atan2 returns a value in radians ) to degrees. Finally, we take our calculated angle and apply it as our rotation. Essentially this will cause our sprite to rotate relative to the mouse location.
Now lets take a look at the heart of our application. Create a new script file ( or rename MySecondApp.js ) called MyThirdApp.js and enter the following code:
var MyThirdApp = cc.LayerColor.extend( { _jetSprite:null, init:function(){ this._super(); this.initWithColor(new cc.Color4B(0,0,0,255)); var size = cc.Director.getInstance().getWinSize(); this._jetSprite = new JetSprite(); this.setTouchEnabled(true); this.setKeyboardEnabled(true); this.setPosition(new cc.Point(0,0)); this.addChild(this._jetSprite); this._jetSprite.setPosition(new cc.Point(size.width/2,size.height/2)); this._jetSprite.scheduleUpdate(); this.schedule(this.update); return true; }, onEnter:function(){ this._super(); }, update:function(dt){ }, onTouchesEnded:function (pTouch,pEvent){ this._jetSprite.handleTouch(pTouch[0].getLocation()); }, onTouchesMoved:function(pTouch,pEvent){ this._jetSprite.handleTouchMove(pTouch[0].getLocation()); }, onKeyUp:function(e){ }, onKeyDown:function(e){ this._jetSprite.handleKey(e); } }); MyThirdAppScene = cc.Scene.extend({ onEnter:function(){ this._super(); var layer = new MyThirdApp(); layer.init(); this.addChild(layer); } })
The bulk of this code is very similar to previous code with one major difference. In previous examples, we created a layer, then added a color layer to it. In this case, we simply extend from LayerColor and add our children directly to ourselves. There is very little functional differences between the two approaches, except this one is a bit more concise. Now let’s take a closer look at our code.
In our init() method, we init with a black background colour. We then create our JetSprite object and tell cocos2D that our layer handles touch and keypad events. In cocos2D, input is handled at the layer level, and setting these two values will result in touch and keyboard delegates being added to our object so we can handle IO events. We then position our layer, add our _jetSprite as a child positioned in the center of our layer. The scheduleUpdate() call is very important. This will result in our JetSprite’s update() function getting called every frame. If you don’t call this method, or sprite wont do anything!
Next up is onEnter() which is what is called when a layer becomes the active layer. In our case, this call is very important, as it ( in cc.Layer.onEnter ) is where the touch and keyboard delegates are actually attached to our layer! If you remove the call to this._super(), you will not receive any keyboard and mouse events!
Now that we are wired up to receive touch and keyboard events, we need a couple methods that will be called when they occur. In our case we are going to use ccTouchesEnded, ccTouchesMoved, keyUp and keyDown. When an event occurs, these methods will be called, which in turn simply call the appropriate method on our JetSprite.
There are two things to be aware of here. There are ccTouch____ and ccTouches____ versions of events; to my experience, the ccTouch____ version will never be called! If you want to handle touch/mouse events, use the ccTouches version. The next is the way we are handling key presses, right now since we used keyDown, holding down the key will result in it being called over and over. If however you want to respond only once per key hit ( regardless to if it is held down or not ), use keyUp instead.
We also need to make sure our new classes are included in our project. Just like the last tutorial, make the following changes to the bottom of cocos2d.js
appFiles:['MyThirdApp.js','JetSprite.js']
We also need to update the Scene name in main.js
var myApp = new cocos2dApp(MyThirdAppScene);
Here is the end result of all of our code. Mouse over the canvas and the mouse will follow you. Left and right arrow will rotate the sprite, while clicking (either button) on the left half of the screen will point the jet up, while clicking the right half will point the sprite down.
If you are running Chrome be aware the clicking doesn’t appear to work. It is actually working, the sprite is being rotated. However it is also triggering a mouse move event, immediately snapping the sprite back to where you mouse pointer is!
As always, you can download the whole project right here. It includes a zip of the cocos2D version I used, in case you have trouble working with the current release.
Read Tutorial 5