- Cocos2d-x Game Development Blueprints
- Karan Sequeira
- 314字
- 2021-07-16 13:47:57
Setting things into motion
We'll need to move the castle and the silhouettes to create an illusion of the dragon flying. We do this by updating their position after every tick, as shown here:
FairytaleManager.prototype.update = function() { this.updateCastle(); this.updateSilhouette(); };
The update
function will be called from the parent layer (MainMenu
or GameWorld
) on every tick. It is here that you will have to move your castle and the backdrop. The updateCastle
and updateSilhouette
functions are identical, so I will discuss the updateCastle
function only:
FairytaleManager.prototype.updateCastle = function(){ for(var i = 0; i < this.castleSprites.length; ++i) { // first update the position based on the scroll speed var castleSprite = this.castleSprites[i]; castleSprite.setPosition(castleSprite.getPositionX() - MAX_SCROLLING_SPEED, castleSprite.getPositionY()); // check if the sprite has gone completely out of the left edge of the screen if(castleSprite.getPositionX() < (this.castleSpriteSize.width * -0.5)) { // reposition it after the last wall sprite var positionX = this.castleSprites[this.lastCastleIndex].getPositionX() + this.castleSpriteSize.width - MAX_SCROLLING_SPEED; castleSprite.setPosition(positionX, castleSprite.getPositionY()); // this sprite now becomes the new last wall this.lastCastleIndex = i; } } };
We loop through each sprite of the castle and shift them left by MAX_SCROLLING_SPEED
pixels. Subsequently, we need to check if a castle sprite has gone completely out of the left edge of the screen. If it has, we need to reposition it at the right end of the screen, next to the sprite that is currently at the end. We already have a variable named lastCastleIndex
that tells us that. After placing this sprite at the end, we also need to update the lastCastleIndex
variable. It is worth noting that the updateSilhouette
function scrolls the silhouette sprites at half the value of MAX_SCROLLING_SPEED
. This is how we will create a perception of depth or parallax.
We achieved the illusion of an infinitely long castle. Okay, you can go ahead and tell the dragon about it now. It's time to start playing the game anyway.
- The DevOps 2.3 Toolkit
- AWS Serverless架構(gòu):使用AWS從傳統(tǒng)部署方式向Serverless架構(gòu)遷移
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優(yōu)化計(jì)算
- Python金融數(shù)據(jù)分析
- YARN Essentials
- C++20高級(jí)編程
- Java Web應(yīng)用開發(fā)給力起飛
- C++程序設(shè)計(jì)
- ASP.NET Web API Security Essentials
- R語言數(shù)據(jù)挖掘:實(shí)用項(xiàng)目解析
- Java EE架構(gòu)設(shè)計(jì)與開發(fā)實(shí)踐
- Java從入門到精通(視頻實(shí)戰(zhàn)版)
- 算法超簡(jiǎn)單:趣味游戲帶你輕松入門與實(shí)踐
- 每個(gè)人的Python:數(shù)學(xué)、算法和游戲編程訓(xùn)練營(yíng)
- Raspberry Pi Robotic Projects