Game/Phaser
Pet_2) inputEnabled, enableDrag(), button 처리
surhommejk
2018. 4. 6. 16:54
inputEnabled, enableDrag()
this.pet = this.game.add.sprite(100, 400, 'pet');
this.pet.anchor.setTo(0.5);
//custom properties
this.pet.customParams = {health: 100, fun: 100};
// The InputEnabled component allows a Game Object
// to have its own InputHandler and process input related events.
this.pet.inputEnabled = true;
// 이 메소드를 통해 pet이 drag가 가능하게 된다
this.pet.input.enableDrag();
this.apple = this.game.add.sprite(72, 570, 'apple');
this.apple.anchor.setTo(0.5);
this.apple.inputEnabled = true;
this.apple.customParams = {health: 20};
// 트리거 발생시 실행될 함수와 실행 범위 설정(context)
this.apple.events.onInputDown.add(this.pickItem, this);
this.candy = this.game.add.sprite(144, 570, 'candy');
this.candy.anchor.setTo(0.5);
this.candy.inputEnabled = true;
this.candy.customParams = {health: -10, fun: 10};
this.candy.events.onInputDown.add(this.pickItem, this);
this.toy = this.game.add.sprite(216, 570, 'toy');
this.toy.anchor.setTo(0.5);
this.toy.inputEnabled = true;
this.toy.customParams = {fun: 20};
this.toy.events.onInputDown.add(this.pickItem, this);
this.rotate = this.game.add.sprite(288, 570, 'rotate');
this.rotate.anchor.setTo(0.5);
this.rotate.inputEnabled = true;
this.rotate.events.onInputDown.add(this.rotatePet, this);
this.buttons = [this.apple, this.candy, this.toy, this.rotate];
//nothing is selected
this.selectedItem = null;
},
// create 영역에 있다는 것을 명심 (update가 아니다)
pickItem: function(sprite, event) {
console.log('pick item');
},
rotatePet: function(sprite, event) {
console.log('rotating..');
}
button 처리
this.buttons = [this.apple, this.candy, this.toy, this.rotate];
//nothing is selected
this.selectedItem = null;
//the user interface (UI) is not blocked at the start
this.uiBlocked = false;
},
pickItem: function(sprite, event) {
//if the UI is blocked we can't pick an item
//true가 아닌 이상 원천적으로 아예 아무 것도 실행되지 않도록 처리
if(!this.uiBlocked) {
console.log('pick item');
// 일단 원하는 로직을 넣고 나중에 밑에 함수를 쓰면 된다
// 이런 식으로 코딩
this.clearSelection();
//alpha to indicate selection
// 투명도 처리
sprite.alpha = 0.4;
// selectedItem을 선언하고 클릭된 sprite를 할당
this.selectedItem = sprite;
}
},
rotatePet: function(sprite, event) {
if(!this.uiBlocked) {
//we want the user interface (UI) to be blocked until the rotation ends
this.uiBlocked = true;
this.clearSelection();
//alpha to indicate selection
sprite.alpha = 0.4;
console.log('rotating..');
}
},
clearSelection: function() {
//remove transparency from all buttons
this.buttons.forEach(function(element, index){
element.alpha = 1;
});
//we are not selecting anything now
this.selectedItem = null;
}
};