Objects created with createObject() in JavaScript disappear immediately and Trigger Once fires repeatedly

Claude and I are building a card game and using a JavaScript event to create Card objects dynamically with runtimeScene.createObject("Card"). The cards are created successfully (confirmed via console.log) but never appear on screen. Also, “Trigger once while true” fires hundreds of times per second instead of once. I have a template Card object placed off-screen at (-200, -200). What am I doing wrong?

It’s easy to create objects using events. I think you will have a better time building your game using events.

Can you post a screenshot of the Javascript or copy and paste it inside a preformated text. (preformated is in the gear menu on the top-right of the menu.

const gameVars = runtimeScene.getGame().getVariables();

const suits = ["spades", "hearts", "clubs", "diamonds"];
const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
let deck = [];
for (let suit of suits) {
    for (let rank of ranks) {
        deck.push(suit + "_" + rank);
    }
}
deck.push("joker_big");
deck.push("joker_little");

for (let i = deck.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [deck[i], deck[j]] = [deck[j], deck[i]];
}

const p1Hand = deck.slice(0, 18);
const p2Hand = deck.slice(18, 36);
const p3Hand = deck.slice(36, 54);

let dealer = "P1";
for (let i = 0; i < 54; i++) {
    if (deck[i].startsWith("diamonds_")) {
        if (i % 3 === 0) dealer = "P1";
        else if (i % 3 === 1) dealer = "P2";
        else dealer = "P3";
        break;
    }
}

gameVars.get("CurrentDealer").setString(dealer);
gameVars.get("p1Hand").setString(JSON.stringify(p1Hand));
gameVars.get("p2Hand").setString(JSON.stringify(p2Hand));
gameVars.get("p3Hand").setString(JSON.stringify(p3Hand));

const fanWidth = 850;
const centerX = 540;
const baseY = 1600;
const fanAngleRange = 30;

for (let i = 0; i < 18; i++) {
    const fanCard = runtimeScene.createObject("Card");
    if (!fanCard) continue;
    const t = i / 17;
    const x = centerX - fanWidth / 2 + t * fanWidth;
    const angle = -fanAngleRange / 2 + t * fanAngleRange;
    fanCard.setX(x);
    fanCard.setY(baseY);
    fanCard.setWidth(120);
    fanCard.setHeight(160);
    fanCard.setAngle(angle);
    fanCard.setAnimationName(p1Hand[i]);
    fanCard.pauseAnimation();
}

console.log("Done. Dealer: " + dealer);
[details="Summary"]
This text will be hidden
[/details]

I tested some of the Javascript. Just the loop and the create object and it worked. Maybe the cards are being placed off screen. What are their positions in the debugger?

Edit: I tried more of the events and it works. I had to change the baseY from 1600 to 600. I tested it in portrait view and the cards were way off screen. I skipped the animations. I’m not sure what you’re going for but it mostly works. The image below was cropped from a tall portrait view.