[SOLVED] sprite cloning

every time i hit the X button nothing happens, the launcher still works but i don’t see a clone of the sprite

PlayerBullit.prototype.update = function(dt) {
    var play= this.app.root.findByName('player');
    var pos = play.getPosition();
    var e = this.entity.clone();
    if(this.app.keyboard.wasPressed(pc.KEY_X)){
        e.enabled=true;
        e.setPosition(pos.x,pos.y,0);
        
    }
    if(this.app.keyboard.wasReleased(pc.KEY_X)){
        e.enabled=false;
    }
};

You will need to add the cloned entity to the scene hierarchy (eg this.app.root.addChild(e)) .

i placed it under the KEY_X command but it crashes the test

Do you have a project link to look at?

https://playcanvas.com/editor/scene/661029

TLDR; there’s an infinite loop happening. As you are cloning the entity where the input is processed, as the clone gets added to the scene hierarchy, it’s update gets called and because it is in the same frame, the keypress is still considered as ‘wasPressed’, it clones itself again, etc forever.

So the solution is to move the logic for the cloning outside the entity you are cloning (ideally in a separate script where all the input can be processed separately from the entity’s logic).

See https://playcanvas.com/editor/scene/661419 for example. Input.js specifically: https://playcanvas.com/editor/code/585203?tabs=15311943

what does it mean by fireballprefab?

It’s just handing a reference to the fireball entity that we are cloning. I’m just using Unity’s terminology to indicate that it is something that will be cloned. (Check the scenes Root entity for the usage of the input script).

just a small question. i want the shots to go in the direction the player faces, but when i think about it, the shots would turn around when the player turns around

What’s the question? :sweat_smile:

this is what i want the shots to do

  1. go in the direction the player faces(without changing directions)
  2. delete when it hits the edge of the screen
  1. As long as the shots are not children of the player entity, they will not be affected by the player changing direction unless you have code that explicitly does so.

  2. If the camera is a moving camera, you can do world to screen to check if it is in view. It may not be the logic you want for the game though and may prefer them to be destroyed after a certain amount of time has passed.

i thougt that just to have entitys outside of the camera and when they touch it they delete themselves

how do you treat flipx as a bool?

if(entityvar.sprite.flipx===true){}