日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scratch跳一跳游戏脚本_cocos creator制作微信小游戏「跳一跳」

發(fā)布時間:2023/12/10 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scratch跳一跳游戏脚本_cocos creator制作微信小游戏「跳一跳」 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、游戲的分析(之前沒有接觸過小游戲,制作的思維還停留在大型ARPG游戲大家共同協(xié)作的想法里,但是小游戲講究小而全,大部分時間是一個人獨立開發(fā),所以需要迫使自己養(yǎng)成看到小游戲先拆分細化的思想)

二、一些必要的參數(shù)

臺階參數(shù)

1: 設(shè)置分辨率: 720x1280

2: 臺階的原點:

3: 配置好4個中心點,如果跳到就到4個中心點,否則就失敗;

4: 編寫代碼獲得中心點的位置,來初始化Player;

5: 斜率: 0.5560472, 與平行四邊形相當(dāng);

6: 塊開始的世界坐標(biāo)的位置: (180, 350)

參數(shù)

1: 監(jiān)聽事件,開啟蓄積能量, 2秒縮放到0.5;

2: 0.5秒內(nèi),scale變形到1;

3: 0.5秒內(nèi), 旋轉(zhuǎn)一周;

4: 0.5秒內(nèi),跳躍到目的地;

5: 能量蓄積: 加速度: power: 600, 初始化: 150;

this.speed += dt * this.power;

this.jumpDistance += this.speed * dt;

6:帶上拖尾:

MotionStreak組件: 配置好圖片;(引擎bug,父節(jié)點移動,帶有拖尾的子節(jié)點顯示效果就會有問題,待解決)

7: ancher_offset: 100

8: 生成地圖: 200, 400的距離隨機

9: 地圖滾動參數(shù): y, 隨著玩家滾動, x 取最左值和最右值(180, 580);

10:開始位置(180, 350)

三、實現(xiàn)的步驟

1 調(diào)整好player,具有旋轉(zhuǎn)和壓縮兩個動畫

2 實現(xiàn)跳躍,前置條件是有東西可跳,所以要先制作兩個塊

3 制作塊,設(shè)置塊的原點,中心點,前后左右點(主要是做跳躍檢測,使用小色塊),currBlock nextBlock

4 開始寫game_scene.js腳本

綁定player和block

固定第一個block(坐標(biāo)轉(zhuǎn)換)

添加第二個block (add_block方法)

5 調(diào)整第一個塊和player的位置

6 實現(xiàn)player的跳躍方法player_jump,以及block的檢測方法is_jump_on_block

7 實現(xiàn)地圖滾動方法move_map

8 完善游戲代碼

四、代碼

game_scene.js

//game_scene.js

cc.Class({

extends: cc.Component,

properties: {

// foo: {

// // ATTRIBUTES:

// default: null, // The default value will be used only when the component attaching

// // to a node for the first time

// type: cc.SpriteFrame, // optional, default is typeof default

// serializable: true, // optional, default is true

// },

// bar: {

// get () {

// return this._bar;

// },

// set (value) {

// this._bar = value;

// }

// },

player: {

type: cc.Node,

default: null

},

block_prefabs: {

type: cc.Prefab,

default: []

},

block_root: {

default: null,

type: cc.Node

},

left_org: cc.v2(0, 0),

map_root: {

default: null,

type: cc.Node

},

y_radio: 0.5560472,

checkout: {

type: cc.Node,

default: null

}

},

// LIFE-CYCLE CALLBACKS:

// onLoad () {},

start() {

this.block_list = [];

this.cur_block = cc.instantiate(this.block_prefabs[Math.floor(Math.random() * this.block_prefabs.length)]);

this.block_root.addChild(this.cur_block);

// 將第一個塊的位置設(shè)置為初始位置

this.cur_block.setPosition(this.block_root.convertToNodeSpaceAR(this.left_org));

var w_pos = this.cur_block.getChildByName('mid').convertToWorldSpaceAR(cc.v2(0, 0));

this.player.setPosition(this.map_root.convertToNodeSpaceAR(w_pos));

this.next_block = this.cur_block;

this.player_comp = this.player.getComponent("player");

this.block_zOrder = -1;

this.add_block();

},

add_block: function() {

this.cur_block = this.next_block;

this.next_block = cc.instantiate(this.block_prefabs[Math.floor(Math.random() * this.block_prefabs.length)]);

this.block_root.addChild(this.next_block);

this.next_block.zIndex = this.block_zOrder;

this.block_zOrder--;

var x_distance = 200 + Math.random() * 200;

var y_distance = x_distance * this.y_radio;

var next_pos = this.cur_block.getPosition();

next_pos.x += x_distance * this.player_comp.direction;

next_pos.y += y_distance;

this.next_block.setPosition(next_pos);

this.player_comp.set_next_block(this.next_block.getComponent("block"));

// 刪除block

this.block_list.push(this.next_block);

if (this.block_list.length >= 5) {

for (var i = 0; i < 2; i++) {

var block = this.block_list.shift();

block.destroy();

}

}

},

// 地圖滾動

move_map(offset_x, offset_y) {

var m1 = cc.moveBy(0.5, offset_x, offset_y);

var end_func = cc.callFunc(function() {

this.add_block();

}.bind(this));

var seq = cc.sequence([m1, end_func]);

this.map_root.runAction(seq);

},

on_checkout_game: function() {

this.checkout.active = true;

},

on_game_again: function() {

cc.director.loadScene("game_scene");

},

// update (dt) {},

});

player.js

// player.js

var game_scene = require("game_scene");

cc.Class({

extends: cc.Component,

properties: {

// foo: {

// // ATTRIBUTES:

// default: null, // The default value will be used only when the component attaching

// // to a node for the first time

// type: cc.SpriteFrame, // optional, default is typeof default

// serializable: true, // optional, default is true

// },

// bar: {

// get () {

// return this._bar;

// },

// set (value) {

// this._bar = value;

// }

// },

init_speed: 150,

a_power: 600,

y_radio: 0.5560472,

game_manager: {

type: game_scene,

default: null

},

},

// LIFE-CYCLE CALLBACKS:

onLoad() {

this.next_block = null;

this.direction = 1;

},

start() {

this.rot_node = this.node.getChildByName("rotate");

this.anima_node = this.rot_node.getChildByName("anima");

this.is_power_mode = false;

this.speed = 0;

this.x_distance = 0;

this.anima_node.on(cc.Node.EventType.TOUCH_START, function(e) {

this.is_power_mode = true;

this.x_distance = 0;

this.speed = this.init_speed;

this.anima_node.stopAllActions();

this.anima_node.runAction(cc.scaleTo(2, 1, 0.5));

}.bind(this), this);

this.anima_node.on(cc.Node.EventType.TOUCH_END, function(e) {

this.is_power_mode = false;

this.anima_node.stopAllActions();

this.anima_node.runAction(cc.scaleTo(0.5, 1, 1));

this.player_jump();

}.bind(this), this);

this.anima_node.on(cc.Node.EventType.TOUCH_CANCEL, function(e) {

this.is_power_mode = false;

this.anima_node.stopAllActions();

this.anima_node.runAction(cc.scaleTo(0.5, 1, 1));

this.player_jump();

}.bind(this), this);

},

update(dt) {

if (this.is_power_mode) {

this.speed += (this.a_power * dt);

this.x_distance += this.speed * dt;

}

},

player_jump: function() {

var x_distance = this.x_distance * this.direction;

var y_distance = this.x_distance * this.y_radio;

var target_pos = this.node.getPosition();

target_pos.x += x_distance;

target_pos.y += y_distance;

// 跳躍時候旋轉(zhuǎn)

this.rot_node.runAction(cc.rotateBy(0.5, -360 * this.direction));

var w_pos = this.node.parent.convertToWorldSpaceAR(target_pos);

var is_game_over = false;

if (this.next_block.is_jump_on_block(w_pos, this.direction)) {

target_pos = this.node.parent.convertToNodeSpaceAR(w_pos);

} else {

is_game_over = true;

}

var j = cc.jumpTo(0.5, target_pos, 200, 1);

this.direction = (Math.random() < 0.5) ? -1 : 1;

var end_func = cc.callFunc(function() {

if (is_game_over) {

this.game_manager.on_checkout_game();

} else {

if (this.direction === -1) {

this.game_manager.move_map(580 - w_pos.x, -y_distance);

} else {

this.game_manager.move_map(180 - w_pos.x, -y_distance);

}

}

}.bind(this));

var seq = cc.sequence(j, end_func);

this.node.runAction(seq);

},

set_next_block(block) {

this.next_block = block;

},

});

block.js

// block.js

cc.Class({

extends: cc.Component,

properties: {

// foo: {

// // ATTRIBUTES:

// default: null, // The default value will be used only when the component attaching

// // to a node for the first time

// type: cc.SpriteFrame, // optional, default is typeof default

// serializable: true, // optional, default is true

// },

// bar: {

// get () {

// return this._bar;

// },

// set (value) {

// this._bar = value;

// }

// },

},

start() {

this.mid = this.node.getChildByName("mid");

this.up = this.node.getChildByName("up");

this.down = this.node.getChildByName("down");

this.left = this.node.getChildByName("left");

this.right = this.node.getChildByName("right");

},

// dir 1右跳 -1左跳

is_jump_on_block(w_dst_pos, direction) {

var mid_pos = this.mid.convertToWorldSpaceAR(cc.v2(0, 0));

var dir = w_dst_pos.sub(mid_pos);

var min_len = dir.mag();

var min_pos = mid_pos;

if (direction === 1) {

var up_pos = this.up.convertToWorldSpaceAR(cc.v2(0, 0));

dir = w_dst_pos.sub(up_pos);

var len = dir.mag();

if (min_len > len) {

min_len = len;

min_pos = up_pos;

}

var down_pos = this.down.convertToWorldSpaceAR(cc.v2(0, 0));

dir = w_dst_pos.sub(down_pos);

var len = dir.mag();

if (min_len > len) {

min_len = len;

min_pos = down_pos;

}

} else {

var left_pos = this.left.convertToWorldSpaceAR(cc.v2(0, 0));

dir = w_dst_pos.sub(left_pos);

var len = dir.mag();

if (min_len > len) {

min_len = len;

min_pos = left_pos;

}

var right_pos = this.right.convertToWorldSpaceAR(cc.v2(0, 0));

dir = w_dst_pos.sub(right_pos);

var len = dir.mag();

if (min_len > len) {

min_len = len;

min_pos = right_pos;

}

}

// 找到跳躍位置距離參考點最近的那個點以及位置

dir = w_dst_pos.sub(min_pos);

if (dir.mag() < 100) {

w_dst_pos.x = min_pos.x;

w_dst_pos.y = min_pos.y;

return true;

}

return false;

},

});

作者:orxx

來源:博客園

聲明:發(fā)布此文是出于傳遞更多知識以供交流學(xué)習(xí)之目的。若有來源標(biāo)注錯誤或侵犯了您的合法權(quán)益,請作者持權(quán)屬證明與我們聯(lián)系,我們將及時更正、刪除,謝謝

總結(jié)

以上是生活随笔為你收集整理的scratch跳一跳游戏脚本_cocos creator制作微信小游戏「跳一跳」的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。