I would like to have a tile-based game area bigger than the viewable area and to be able to move the camera as the avatar moves around. I have read that I can do that using cc.Camera
or cc.Follow
. Where can I find an example how to use any of them in JavaScript? And what is the difference between cc.Camera
or cc.Follow
?
you can use cc.Follow for what your looking to achieve you just need to create a follow action and run it on the main layer of your scene sample code:
var followAction = cc.Follow.create(this.mPlayer.shape.image,cc.rect(0,0,mapWidth,mapHeight));
this.gameLayer.runAction(followAction);
This will then keep the “camera” only following your player inside the rect you specify
What is mPlayer.shape.image
in this example? Is that the avatar sprite? Where should I put this code? Should it be in the init()
method of the layer containing the avatar? Is there anywhere a working example, I could analyze?
mPlayer is a player object the shape/image is the sprite attached to the physicsbody.
yes you can put this in your init function.
For a working example if you look in cocos2d-js/samples/actionsTest.js there is a follow test which uses this action if you run the project you can see it working