- Learning Cocos2d-JS Game Development
- Emanuele Feronato
- 177字
- 2021-08-06 19:43:53
Removing images and changing the background color
Now you know how to add images you might also be interested in knowing how to remove them. It's really intuitive: you added images with the addChild
method, so you are going to remove them with the removeChild
method.
Moreover, we will change the background color by adding an actual background layer, which covers the entire scene with a solid color.
There are just a couple of lines to add to gamescript.js
:
var gameScene = cc.Scene.extend({ onEnter:function () { this._super(); var gameLayer = new game(); gameLayer.init(); this.addChild(gameLayer); } }); var backgroundLayer; var game = cc.Layer.extend({ init:function () { this._super(); backgroundLayer = cc.LayerColor.create(new cc.Color(40,40,40,255), 320, 480); this.addChild(backgroundLayer); var target = cc.Sprite.create("assets/target.png"); backgroundLayer.addChild(target,0); target.setPosition(160,240); setTimeout(function(){ backgroundLayer.removeChild(target); }, 3000); } });
In the preceding code, backgroundLayer
is a new layer that will be filled with a new color with the RGBA format (in this case, a full opaque dark grey), which will also contain the target image.
After three seconds since its creation, the target is removed from backgroundLayer
with the removeChild
method.
推薦閱讀
- The Supervised Learning Workshop
- Microsoft Dynamics 365 Extensions Cookbook
- 單片機(jī)C語言程序設(shè)計(jì)實(shí)訓(xùn)100例:基于STC8051+Proteus仿真與實(shí)戰(zhàn)
- Vue.js快速入門與深入實(shí)戰(zhàn)
- 樂學(xué)Web編程:網(wǎng)站制作不神秘
- Dependency Injection in .NET Core 2.0
- NumPy Essentials
- Python編程完全入門教程
- Full-Stack React Projects
- Java網(wǎng)絡(luò)編程實(shí)戰(zhàn)
- Kubernetes進(jìn)階實(shí)戰(zhàn)
- QGIS 2 Cookbook
- Java Web應(yīng)用開發(fā)給力起飛
- 計(jì)算機(jī)系統(tǒng)解密:從理解計(jì)算機(jī)到編寫高效代碼
- 游戲設(shè)計(jì)的底層邏輯