Making instances of an object as in enemies in a game

Is there a way to make multiple instances of one object? Like enemies in a game or bullets? Tried looking for it in the forums before, but didn’t find anything like it… Is it possible? Do I have to use a dirty hack, or wick isnt oriented to that kind of stuff? I mean to do this programatically of course…

2 Likes

The next version of Wick will have this feature. :grinning: A preview of the new version will be released on the 24th.

2 Likes

Yes there is a way but it’s a bit limited at the moment.

Click on your clip you want to instance
To make a new instance of it you can use:

var newclone = this.clone();
newclone.x = 100;
newclone.y = 100;

To clear all clones of an object you can do this.clones.length = 0

You have to get a bit creative because the original clip you are instancing will also run the code. An example of this is in my recent Game Example project I uploaded, I wanted a rectangle to go from the left side of the screen to the right and disappear, but a problem I ran into was that the original clip would go and disappear, bugging the cloning system out. To fix this I added the line of code

if (this.x > 0) {

and I placed the rectangle outside of the canvas to the left and it worked. So a quick example of how this would work:

var toclone = project.getObject(‘Enemy’);
function load() {
var n = toclone.clone();
n.x = 100;
n.y = 200;
}

And inside of the object you want to clone:

function update() {
if (this.x > 0) {
this.x += 1;
}
}

I’ve attached an example project I’ve made quickly for further help (most of the code is in the frame):
Clone Example-Oct18-2018-5.30PM.wick (3.8 KB)

2 Likes

ty for the info! i’ll be expectant!

1 Like

i did thought of pure js option, one question does this clone method adds the instance to an array inside the original object? is it native from js or a third party lib that wick uses? anyway thank you for responding and for the workaround

2 Likes

Ok, so I’ve experimented with it a bit, (yes i know now it is wick’s method) there is a IsClone property that can check if the object is a clone (duh). ty again for your response

2 Likes

The isClone property definitely makes things a lot easier :laughing: and you’re welcome!

1 Like

@MisterMender I have a doubt with the clone() method, does this also clone that object’s methods if any was written in its load methods?

I am trying to use the methods that I defined in a object, upon cloning it, but I am not able to.