+ "code": "// Animate Sprites with platformer movement [๐๏ธ]\n\n// This example may be large, so use regions for navigating faster. Important\n// content is marked with ๐๏ธ\n\nkaplay({ scale: 4, font: \"happy\" });\n\nconst SPEED = 120;\nconst JUMP_FORCE = 240;\nsetGravity(640);\n\n// #region Loading Assets ๐๏ธ\nloadBitmapFont(\"happy\", \"/fonts/happy_28x36.png\", 28, 36);\n\n// Loading a multi-frame sprite ๐๏ธ\nloadSprite(\"dino\", \"/sprites/dungeon-dino.png\", {\n // The image contains 9 frames layered out horizontally, slice it into individual frames\n sliceX: 9,\n // Define animations\n anims: {\n \"idle\": {\n // Starts from frame 0, ends at frame 3\n from: 0,\n to: 3,\n // Frame per second\n speed: 5,\n loop: true,\n },\n \"run\": {\n from: 4,\n to: 7,\n speed: 10,\n loop: true,\n },\n // This animation only has 1 frame\n \"jump\": 8,\n },\n});\n// #endregion\n\n// #region Game Objects\n\n// Add our player character ๐๏ธ\nconst player = add([\n sprite(\"dino\"),\n pos(center()),\n anchor(\"center\"),\n area(),\n body(),\n]);\n\n// Add a platform\nadd([\n rect(width(), 24),\n area(),\n outline(1),\n pos(0, height() - 24),\n body({ isStatic: true }),\n]);\n// #endregion\n\n/* ๐๏ธ\nWe can animate sprites using obj.play(\"name\") method.\n\nThis time we're defining a function for executing animations conditionally.\n*/\n\n// #region Player animations ๐๏ธ\nconst playerPlayRun = () => {\n // obj.play() will reset to the first frame of the animation\n // so we want to make sure it only runs when the current animation is not \"run\"\n if (player.isGrounded() && player.getCurAnim().name !== \"run\") {\n player.play(\"run\");\n }\n};\n\nconst playerPlayIdle = () => {\n // Only reset to \"idle\" if player is not holding any of these keys\n if (player.isGrounded() && !isKeyDown(\"left\") && !isKeyDown(\"right\")) {\n player.play(\"idle\");\n }\n};\n// #endregion\n\n// #region Player move/anim ๐๏ธ\nonKeyDown(\"left\", () => {\n player.move(-SPEED, 0);\n player.flipX = true;\n playerPlayRun();\n});\n\nonKeyDown(\"right\", () => {\n player.move(SPEED, 0);\n player.flipX = false;\n playerPlayRun();\n});\n\nonKeyRelease([\"left\", \"right\"], () => {\n playerPlayIdle();\n});\n\nonKeyPress([\"space\", \"up\"], () => {\n if (player.isGrounded()) {\n player.jump(JUMP_FORCE);\n player.play(\"jump\");\n }\n});\n\n// Switch to \"idle\" or \"run\" animation when player hits ground\nplayer.onGround(() => {\n if (!isKeyDown(\"left\") && !isKeyDown(\"right\")) {\n player.play(\"idle\");\n }\n else {\n player.play(\"run\");\n }\n});\n\n// #endregion\n\n// You can run functions when a specific animation ends ๐๏ธ\nplayer.onAnimEnd((anim) => {\n if (anim === \"idle\") {\n debug.log(\"hi!\");\n }\n});\n\n// #region UI\nconst getInfo = () =>\n `\nAnim: ${player.getCurAnim()?.name}\nFrame: ${player.frame}\n`.trim();\n\n// Add some text to show the current animation\nconst label = add([\n text(getInfo(), { size: 12 }),\n color(0, 0, 0),\n pos(4),\n]);\n\nlabel.onUpdate(() => {\n label.text = getInfo();\n});\n// #endregion",
0 commit comments