I don’t understand how to properly collide two objects. I am using spine objects.
I want the gift object to disappear when the hero collides with the object.
I tried different ways, but it doesn’t work. maybe I went wrong in some place, and this code is wrong …
var HeroControls = pc.createScript("heroControls");
HeroControls.prototype.initialize = function () {
this.state = "idle";
};
HeroControls.prototype.update = function (dt) {
var keyboard = this.app.keyboard;
var spine = this.entity.spine;
if (keyboard.isPressed(pc.KEY_SPACE)) {
if (this.state !== "attack") {
spine.state.setAnimation(0, "bunny_with_gun", true);
this.state = "attack";
}
} else if (keyboard.wasPressed(pc.KEY_DOWN) || app.keyboard.isPressed(pc.KEY_S)) {
if (this.state !== "down") {
spine.state.setAnimation(1, "down", false);
this.state = "down";
console.log("dkljsfoasdhf")
}
} else if (keyboard.isPressed(pc.KEY_UP)|| app.keyboard.isPressed(pc.KEY_W)) {
if (this.state !== "up") {
spine.state.setAnimation(2, "jump_slow", false);
spine.state.setAnimation(3, "run_slow", true);
this.state = "up";
}
} else {
if (this.state !== "idle") {
spine.state.setAnimation(0, "bunny_idle", true);
this.state = "idle";
}
}
}
// Create the hero character
var hero = new pc.Entity();
hero.addComponent("spine", {
atlasAsset: assets[1].id,
skeletonAsset: assets[2].id,
textureAssets: [ assets[3].id ]
});
hero.addComponent("script");
hero.script.create("heroControls");
app.root.addChild(hero);
hero.translate(-4, -2, -2);
hero.spine.state.defaultMix = 0.2;
hero.spine.state.data.setMix("bunny_idle", "run_slow", 0.2);
hero.spine.state.data.setMix("bunny_idle", "bunny_with_gun", 0.2);
hero.spine.spine.skeleton.setSlotsToSetupPose();
hero.spine.state.setAnimation(0, "bunny_idle", true);
hero.spine.state.data.skeletonData.animations[0];
hero.spine.states[1] = new spine.AnimationState(hero.spine.spine.stateData);
btn_play.addEventListener('click', function(){
bg.spine.state.setAnimation(0, "fence_base_slow", true);
// gift
var giftControls = pc.createScript("giftControls");
giftControls.prototype.initialize = function() {
this.leftSide = false;
this.secsSinceLastFlip = 1;
}
giftControls.prototype.update = function(dt) {
if (this.secsSinceLastFlip > -10) {
this.leftSide = !this.leftSide;
this.secsSinceLastFlip = 0;
}
if(this.leftSide){
this.entity.translate(-12*dt,0*dt,0);
} else{
this.entity.translate(1*dt,0*dt,0);
}
this.secsSinceLastFlip += dt;
}
var gift = new pc.Entity();
gift.addComponent("spine", {
atlasAsset: assets[8].id,
skeletonAsset: assets[9].id,
textureAssets: [ assets[10].id]
});
gift.addComponent("script");
gift.script.create("giftControls");
app.root.addChild(gift);
gift.translate(13, -3, -1);
gift.spine.states[1] = new spine.AnimationState(gift.spine.spine.stateData);
gift.spine.state.setAnimation(0, "gift_idle", true);
gift.spine.spine.priority = 2;
hero.spine.state.setAnimation(0, "run_slow", true);
document.getElementById("btn_play").style.display = 'none';
});
hero.spine.spine.priority = 3; var HeroControls = pc.createScript("heroControls");
HeroControls.prototype.initialize = function () {
this.state = "idle";
};
HeroControls.prototype.update = function (dt) {
var keyboard = this.app.keyboard;
var spine = this.entity.spine;
if (keyboard.isPressed(pc.KEY_SPACE)) {
if (this.state !== "attack") {
spine.state.setAnimation(0, "bunny_with_gun", true);
this.state = "attack";
}
} else if (keyboard.wasPressed(pc.KEY_DOWN) || app.keyboard.isPressed(pc.KEY_S)) {
if (this.state !== "down") {
spine.state.setAnimation(1, "down", false);
this.state = "down";
}
} else if (keyboard.isPressed(pc.KEY_UP)|| app.keyboard.isPressed(pc.KEY_W)) {
if (this.state !== "up") {
spine.state.setAnimation(2, "jump_slow", false);
spine.state.setAnimation(3, "run_slow", true);
this.state = "up";
}
} else {
if (this.state !== "idle") {
spine.state.setAnimation(0, "bunny_idle", true);
this.state = "idle";
}
}
}
// Create the hero character
var hero = new pc.Entity();
hero.addComponent("spine", {
atlasAsset: assets[1].id,
skeletonAsset: assets[2].id,
textureAssets: [ assets[3].id ]
});
hero.addComponent("script");
hero.script.create("heroControls");
app.root.addChild(hero);
hero.translate(-4, -2, -2);
hero.spine.state.defaultMix = 0.2;
hero.spine.state.data.setMix("bunny_idle", "run_slow", 0.2);
hero.spine.state.data.setMix("bunny_idle", "bunny_with_gun", 0.2);
hero.spine.spine.skeleton.setSlotsToSetupPose();
hero.spine.state.setAnimation(0, "bunny_idle", true);
hero.spine.state.data.skeletonData.animations[0];
hero.spine.states[1] = new spine.AnimationState(hero.spine.spine.stateData);
btn_play.addEventListener('click', function(){
// gift
var giftControls = pc.createScript("giftControls");
giftControls.prototype.initialize = function() {
this.leftSide = false;
this.secsSinceLastFlip = 1;
}
giftControls.prototype.update = function(dt) {
if (this.secsSinceLastFlip > -10) {
this.leftSide = !this.leftSide;
this.secsSinceLastFlip = 0;
}
if(this.leftSide){
this.entity.translate(-12*dt,0*dt,0);
} else{
this.entity.translate(1*dt,0*dt,0);
}
this.secsSinceLastFlip += dt;
}
var gift = new pc.Entity();
gift.addComponent("spine", {
atlasAsset: assets[8].id,
skeletonAsset: assets[9].id,
textureAssets: [ assets[10].id]
});
gift.addComponent("script");
gift.script.create("giftControls");
app.root.addChild(gift);
gift.translate(13, -3, -1);
gift.spine.states[1] = new spine.AnimationState(gift.spine.spine.stateData);
gift.spine.state.setAnimation(0, "gift_idle", true);
gift.spine.spine.priority = 2;
hero.spine.state.setAnimation(0, "run_slow", true);
document.getElementById("btn_play").style.display = 'none';
});
hero.spine.spine.priority = 3;