前回ジャンプの動きを作成しました。押せばジャンプするやつですね。
今回はそれのソース(スクリプト)を作成していきます。
ついでにジャンプ音をつける作業をしていきます。
ちなみにうんにょ君は手書きですが、ジャンプ音はフリーの使用しています。
いうて、ブログなんで伝わらないと思いますが笑
AssetsのScriptsフォルダに新規でスクリプトを追加します。
”Player”と名付けます。これからうんにょ君に対する支持はここから出していくことになりそうです。
Player.js
cc.Class({
extends: cc.Component,
properties: {
// ジャンプの滞空時間
jumpTime: 0.5,
// ジャンプの高さ
jumpHeight: 200,
// ジャンプしてるかフラグ
_isJump: false,
// ジャンプ音
jumpSound: {
default: null,
url: cc.AudioClip
}
},
// use this for initialization
onLoad: function () {
// タッチリスナーの初期化
this.setTouchEvent();
},
// タッチイベントの定義
setTouchEvent: function () {
var self = this;
// タッチ一回
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouch: true,
// タッチ開始
onTouchBegan: function (touch, event) {
// ジャンプ
self.playerJump();
return true;
}
}, self.node);
},
playerJump: function () {
// ジャンプ中は反応しない
if (this._isJump) {
return;
}
// ジャンプ音再生
cc.audioEngine.play(this.jumpSound);
// アニメpションをジャンプ用に変更する
var anim = this.node.getComponent(cc.Animation);
anim.play('jumping');
// ジャンプ中
this._isJump = true;
var self = this;
var jumpAction = cc.jumpBy(this.jumpTime,0,0,this.jumpHeight,1);
// 着地したらフラグ解除
var finFunction = cc.callFunc(function() {
self._isJump = false;
// 走るアニメーションに戻す
anim.play('running')
}, this);
// ジャンプ実行
this.node.runAction(cc.sequence(jumpAction, finFunction));
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
propertiesにジャンプの音を入れたので、ヒエラルキーのplayerをクリックしてお好きな音をAssetsからD&Dしてアサインしてあげてください。
これで華麗にジャンプした際に音がなるようになり、少しゲームっぽくなりました。
ちなみに私は大体無音でゲームするので音はいらない派ですww


コメント