1

I want a parameter of one of my classes to be a function that will be particular to an instance of that class. In this case I have a sprite class, I want different "onCollide" functions to be called for different instances of this class. Here is what I have coded.

function StaticSprite(tileset, x, y, onCollide) {
    this.tileset = tileset;
    this.x = x;
    this.y = y;
    this.onCollide = onCollide;
}

var introSign = new StaticSprite(mainTiles, 800, 600, showMessage('Hi.'));

if (isColliding(player, introSign)) {
    introSign.onCollide();
}

This code obviously doesn't work but should at least show what I'm trying to achieve. Thanks.

1 Answer 1

6

You just need to pass it a function, not execute a function and pass the result:

new StaticSprite(..., function () { showMessage('Hi.'); })
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.