Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update on() example which was incorrect #506

Merged
merged 1 commit into from
Nov 11, 2024

Conversation

dragoncoder047
Copy link
Contributor

No description provided.

Copy link

pkg-pr-new bot commented Nov 11, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/kaplayjs/kaplay@506

commit: d09257c

@mflerackers
Copy link
Member

The problem is that the callback is attached using the global on, but the trigger is called on the local npc object. Since there is no global trigger function, the on should have been called on the npc.

@dragoncoder047
Copy link
Contributor Author

the on should have been called on the npc.

No, it works this way too, and you don't need a reference to the npc.

Full example:

kaplay();
loadBean();

const npc = add([
    sprite("bean"),
    pos(100, 100),
    "npc"
]);

on("talk", "npc", (npc, message) => {
    npc.add([
        text(message),
        pos(0, -50),
        lifespan(2),
        opacity(),
    ])
});

onKeyPress("space", () => {
    npc.trigger("talk", "Hello, KAPLAY!");
});

onUpdate(() => {
    npc.pos = vec2(100, 100).add(Vec2.fromAngle(time() * 100).scale(50));
})

@lajbel lajbel merged commit 744d459 into kaplayjs:master Nov 11, 2024
2 checks passed
@dragoncoder047 dragoncoder047 deleted the patch-2 branch November 11, 2024 16:34
lajbel pushed a commit that referenced this pull request Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: DONE
Development

Successfully merging this pull request may close these issues.

3 participants