Hello! I am trying to spawn an entity programatically. It is going great until the very last part where I add the Platform Collision Script. If I do these manually in the editor, it all works great but doing it programmatically makes the script not detect collision. Let me know if anyone has any ideas. I know that the script is being added because I’ve console logged and gotten it to fire correctly.
Here is the entity being created:
PlatformManager.prototype.spawnPlatform = function(spriteName) {
//creates entity, adds sprite, makes pixels correct scale
var platform = new pc.Entity();
platform.setLocalScale(1.5,1.5,1.5);
platform.addComponent('sprite', {
type: pc.SPRITETYPE_SIMPLE,
spriteAsset: this.app.assets.find(spriteName)
});
//depending on which sprite is chosen, changes the vertical and horizontal collision
//and adds them to the platform's hierarchy
var verticalCollision = new pc.Entity();
var lSideCollision = new pc.Entity();
var rSideCollision = new pc.Entity();
if(spriteName === "platform1"){
verticalCollision.setLocalPosition(this.platOneVCollision);
lSideCollision.setLocalPosition(this.platOneLCollision);
rSideCollision.setLocalPosition(this.platOneRCollision);
}
else if(spriteName === "platform2"){
verticalCollision.setLocalPosition(this.platTwoVCollision);
lSideCollision.setLocalPosition(this.platTwoLCollision);
rSideCollision.setLocalPosition(this.platTwoRCollision);
}
else if(spriteName === "platform3"){
verticalCollision.setLocalPosition(this.platThreeVCollision);
lSideCollision.setLocalPosition(this.platThreeLCollision);
rSideCollision.setLocalPosition(this.platThreeRCollision);
}
platform.addChild(verticalCollision);
platform.children[0].addChild(lSideCollision);
platform.children[0].addChild(rSideCollision);
//ISSUE HERE!!!
//adds a script that reads these collisions and fires events based on them
platform.addComponent('script');
platform.script.create("platformCollision");
this.entity.addChild(platform);
};
And here is the Platform Collision Script:
var PlatformCollision = pc.createScript('platformCollision');
// initialize code called once per entity
PlatformCollision.prototype.initialize = function() {
this.isPlayerFalling = true;
this.LPlayerCollision = this.app.root.findByName("lPlayerCollision").getPosition().x;
this.RPlayerCollision = this.app.root.findByName("rPlayerCollision").getPosition().x;
this.verticalPlayerCollision = this.app.root.findByName("verticalPlayerCollision").getPosition().y;
this.singleFireTimerMax = 1;
this.singleFireTimer = this.singleFireTimerMax;
this.app.on('notFalling',function(){
this.isPlayerFalling = false;
},this);
this.app.on('falling',function(){
this.isPlayerFalling = true;
},this);
};
// update code called every frame
PlatformCollision.prototype.update = function(dt) {
this.verticalPlayerCollision = this.app.root.findByName("verticalPlayerCollision").getPosition().y;
//checks vertical with the child of the platform which is vertical collision
if(Math.abs(this.verticalPlayerCollision - this.entity.children[0].getPosition().y) < 0.04){
//checks horizontal with the children of the child of the platform which is horizontal collisions
this.LPlayerCollision = this.app.root.findByName("lPlayerCollision").getPosition().x;
this.RPlayerCollision = this.app.root.findByName("rPlayerCollision").getPosition().x;
var leftPlatCollision = this.entity.children[0].children[0].getPosition().x;
var rightPlatCollision = this.entity.children[0].children[1].getPosition().x;
if(((this.LPlayerCollision > leftPlatCollision) && (this.LPlayerCollision < rightPlatCollision)) ||
((this.RPlayerCollision > leftPlatCollision) && (this.RPlayerCollision < rightPlatCollision))){
if(this.isPlayerFalling === true){
if(this.singleFireTimer >= this.singleFireTimerMax){
this.app.fire('normalJump');
this.app.fire('notFalling');
this.singleFireTimer = 0;
}
}
}
}
if(this.singleFireTimer < this.singleFireTimerMax){
this.singleFireTimer += dt;
}
};
And here is a screenshot of the hierarchy that works within the editor :