Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

All of the properties in a behavior class can be accessed from within itself by using "this.propertyName". The actor it belongs to can be accessed with "this.actor". If you want a more advanced way of setting up an actor when created, you can add awake() which happens instantly, or start() which happens in the next "tick" of game logic. They are used in the same way update() is, but only called once.

class ballBehavior extends Sup.Behavior{
    private angle: number = 0;
    private position: Sup.Math.Vector3 = new Sup.Math.Vector3();
    private velocity: number = 0;

    awake() {
        // You can add code here that will be called only once when it's created.
    }

    update(){
        this.angle += this.velocity;
        
        //Constrains the angle between 0 and 2pi
        if(this.angle > Math.PI * 2) this.angle = 0 + this.angle - Math.PI * 2;
        }else if (this.angle < 0) this.angle = Math.PI - this.angle + Math.PI * 2;
        
        //Maybe a little spinback on the pointer?
        this.position = new Sup.Math.Vector3(Math.cos(this.angle) * wheelActor.getLocalScaleX() * 100 / 100, Math.sin(this.angle) * wheelActor.getLocalScaleX() * 100 / 100, 0);
            
        if(Sup.Input.wasKeyJustPressed("LEFT")) this.velocity = 0.5;
    }
}

If you want to create an actor with a behavior, you just need to use addBehavior() to give it one. You can even change it's default properties too.

// Create a new ball with a behavior.
new Sup.Actor("Ball").addBehavior(ballBehavior);

// Create a new ball with a behavior that starts with altered properties. (almost like a constructor, but every property is optional)
new Sup.Actor("Ball").addBehavior(ballBehavior, {
    angle: Math.PI,
    position: new Sup.Math.Vector3(2, 2, 2),
    velocity: 10
});

// Create a new ball with a behavior that has a different velocity. (properties are optional, so you only need to change the ones you want)
new Sup.Actor("Ball").addBehavior(ballBehavior, {velocity: 10});

If you want to modify the properties after creation, getBehavior() allows you to access the properties in an actor.

// You can do something like this...
let actor = Sup.getActor("Ball");
actor.getBehavior(ballBehavior).velocity = 0;

// Or this...
let actor = Sup.getActor("Ball").getBehavior(ballBehavior);
actor.velocity = 0;

Hope this helps! :)

(+1)

Thanks! I ended up figuring this out when I realized that it wasn't pure JS that added the ability to have classes, it was TypeScript... so I was digging through the wrong documentation entirely. My issue is that I wasn't defining variables properly with name: type, and was instead doing let name = type;