XML/HTML Code復(fù)制內(nèi)容到剪貼板
- LInit(30, 'mydemo', 700, 480, main);
- // 移動(dòng)方向,null代表沒移動(dòng)
- var direction = null;
- // 小鳥,舞臺(tái)層,背景對(duì)象
- var bird, stageLayer, bg;
- // 每次移動(dòng)的長(zhǎng)度
- var step = 5;
- function main () {
- // 資源列表
- var loadList = [
- {name : 'bird', path : './bird.png'},
- {name : 'bg', path : './bg.jpg'}
- ];
- // 加載資源
- LLoadManage.load(loadList, null, demoInit);
- }
- function demoInit (result) {
- // 初始化舞臺(tái)層
- stageLayer = new LSprite();
- addChild(stageLayer);
- // 加入背景
- bg = new LBitmap(new LBitmapData(result['bg']));
- bg.y = -100;
- stageLayer.addChild(bg);
- // 加入小鳥
- bird = new LBitmap(new LBitmapData(result['bird']));
- bird.x = 100;
- bird.y = 150;
- stageLayer.addChild(bird);
- // 添加鼠標(biāo)按下事件
- stageLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onDown);
- // 添加鼠標(biāo)彈起事件
- stageLayer.addEventListener(LMouseEvent.MOUSE_UP, onUp);
- // 添加時(shí)間軸事件
- stageLayer.addEventListener(LEvent.ENTER_FRAME, onFrame);
- }
- function onDown (e) {
- /** 根據(jù)點(diǎn)擊位置設(shè)置移動(dòng)方向 */
- if (e.offsetX > LGlobal.width / 2) {
- direction = 'right';
- } else {
- direction = 'left';
- }
- }
- function onUp () {
- // 設(shè)置方向?yàn)闊o(wú)方向,代表不移動(dòng)
- direction = null;
- }
- function onFrame () {
- var _step, minX, maxX;
- /** 移動(dòng)小鳥 */
- if (direction == 'right') {
- _step = step;
- } else if (direction == 'left') {
- _step = -step;
- } else {
- return;
- }
- bird.x += _step;
- /** 控制小鳥移動(dòng)范圍 */
- minX = 0,
- maxX = bg.getWidth() - bird.getWidth();
- if (bird.x < minX) {
- bird.x = minX;
- }else if (bird.x > maxX) {
- bird.x = maxX;
- }
- /** 移動(dòng)舞臺(tái) */
- stageLayer.x = LGlobal.width / 2 - bird.x;
- /** 控制舞臺(tái)移動(dòng)范圍 */
- minX = LGlobal.width - stageLayer.getWidth(),
- maxX = 0;
- if (stageLayer.x < minX) {
- stageLayer.x = minX;
- }else if (stageLayer.x > maxX) {
- stageLayer.x = maxX;
- }
- }
運(yùn)行結(jié)果: