I need help (giving other objects the same code)

is it possible to give code to other objects without having to type it all again, over and over

example, I have a platform in a game that the player can jump on and moves when the paly touches the side of the screen, I want to add more platforms, can I do that with making a bunch to separate platforms with the same code written over and over .

( i have heard about parenting object but I have found no helpful guides)

Try to search in youtube about Object Oriented Programming for JavaScript. That will help you a lot on how to reuse code.

Now meanwhile, just do a platform with the code on it, and then clone it progrmatically.

var newPlat = platform.clone();
newPlat.x = newXValue... ;

It sounds like you are thinking of clones, which is different from parent objects. Say you draw a rectangle and make it a clip, and call it platform. This is the basic code to create a clone:

newPlatform = platform.clone();

A clone starts out as a copy of the original object. To make multiple platforms, you would just repeat this more times:

platform1=platform.clone();
platform2=platform.clone();
platform3=platform.clone();

Then in your platform clip’s default script, you write this:

if(this.isClone){
//code that applies to all platforms goes here
}

The “this.isClone” code checks if an object is a clone, and if it is, it applies some code. This way all clones can share the same code. Any code that goes here will automatically apply to every cloned object. Also, you can still change clones individually by using the names you assign them, like this:

platform1.x=100;
platform2.x=200;

But any code that goes into the “this.isClone” area will apply to all clones.
(Let me know if any parts of this are confusing, I can try to explain it in different ways)

1 Like

I will go ahead and say thank you for this excellent summary…

(So at least you could say you are welcome to me… :laughing: :face_with_hand_over_mouth:)

my code says

if (this.hitTest§) {
P.y-=20
}

So when it is a clone it just constantly gets higher is their a fix

I will try to help, but admittedly platformers are not something I am super experienced with coding. But I think maybe you could try something like having the player’s y value be equal to how high the platform is, rather than using y-=20. Is this code is to make the player stand on top of the platform?

yes
also do you have a more visual explanation of clones

I can make a picture, one moment

Here’s some screenshots of the basic process. First, I draw a rectangle.
Image2
Select it, click make clip. Then give it a name.
Image3

Image4

Then I go to the default script of the main timeline first frame, and add code to clone the platform twice. I also changed the x value of the clones so they would not be on top of each other.
Image7
Then when the project runs, this is the result:
Image8
The rectangle on the left is the original object. The two others are the clones.

To add custom code for the clones, you click on the original object, then click the default script button for it:
Image9
Then add the code you want to apply to all clones:


Result of adding rotation code that applies to all clones:
ImageNewpng

I tried this and it is not working
Screenshot 2020-11-07 202307
Screenshot 2020-11-07 202316

One thing I think you should change is, it looks like you are putting the code to make the clone in the default script of your clip. I think you should put that code in the default script of the main timeline frame instead. You’d click on the frame to select it, and when it has a blue border like this, you click on the Default script button that appears at the right of the screen.
abc
I think if you put your Pl1=PlatClip.clone() code there, the project will be less prone to errors.
For the other issue, can you explain how In.pause works a bit? Is that when the player is not moving at all?

I didn’t even get to that part

Screenshot 2020-11-07 203520

I’ll work on some code to have the object end up on top of the platform. Is there anything else that isn’t working for you right now?

no, I am starting to just think making a bunch of copy and paste is the easier route

It’s up to you. Personally I think these issues aren’t because of the clones, it’s just that programming platformers can be a bit complicated. I don’t want to discourage you from doing that though.
On a side note, there are some platformer projects that people have shared on the forum, that you could try to study from. If you search for platformer engine, you can find a few examples.

Also, here is the right code to put the player on top of the platform.

if(this.hitTest(player)){
player.y=(this.y-this.height/2)-(player.height/2);
}