Game/Phaser
Pet_3) rotate(), Placing items
surhommejk
2018. 4. 6. 17:28
rotate()
rotatePet: function(sprite, event) {
if(!this.uiBlocked) {
this.uiBlocked = true;
this.clearSelection();
//alpha to indicate selection
sprite.alpha = 0.4;
// tween animation 객체를 선언 후 할당
// tween 함수의 파라미터는 tween animation이 작동할 대상
var petRotation = this.game.add.tween(this.pet);
// make the pet do two loops
// petRotation의 작동 모습과 작동 시간 설정
petRotation.to({angle: '+720'}, 1000);
// petRotation이 끝나면 작동할 함수 설정
petRotation.onComplete.add(function(){
// release the UI
// 타 버튼도 작동할 수 있도록 boolean 처리
this.uiBlocked = false;
sprite.alpha = 1;
//increse the fun of the pet
this.pet.customParams.fun += 10;
console.log(this.pet.customParams.fun);
}, this);
//start the tween animation
petRotation.start();
}
},
Placing items
create: function() {
this.background = this.game.add.sprite(0, 0, 'backyard');
// background에 아이템을 두어야 하므로 background를 onInputDown설정
this.background.inputEnabled = true;
this.background.events.onInputDown.add(this.placeItem, this);
placeItem: function(sprite, event) {
if(this.selectedItem && !this.uiBlocked) {
// event 발생시의 좌표값을 받아와서 할당
var x = event.position.x;
var y = event.position.y;
// 새로운 sprite를 선언하고 받아온 값으로 할당
// 선택된 아이템의 key를 설정
var newItem = this.game.add.sprite(x, y, this.selectedItem.key);
newItem.anchor.setTo(0.5);
// 새로 placing 될 item의 customParams를
// 버튼의 customParams와 똑같이 복제
newItem.customParams = this.selectedItem.customParams;
}
}
Pet의 이동과 기존 아이템 삭제 및 수치 증감처리
var petMovement = this.game.add.tween(this.pet);
petMovement.to({x: x, y: y}, 700);
petMovement.onComplete.add(function(){
//destroy the apple/candy/duck
newItem.destroy();
//release the ui
this.uiBlocked = false;
var stat;
for(stat in newItem.customParams) {
// we only want the properties of the customParams object,
// not properties that may existing in customParams.prototype
// this filters out all non-desired properties
if(newItem.customParams.hasOwnProperty(stat)) {
this.pet.customParams[stat] += newItem.customParams[stat];
}
}
}, this);
//start the tween animation
petMovement.start();
}
}