xxxxxxxxxx
112
/* 11/11/24 progress
adding animation to character
Taylor Ith
*/
let player = Player();
let ninjastar;
// 0 = is idle
// 1 = walk
// global variable to contain image data
//accessed by preload and draw
let charIdleSprite, charWalkSprite;
let ninjastar1move;
// preload loads external media files
//runs once before setup is called
function preload() {
charIdleSprite = loadImage("Sprite/character_Idle.gif");
charWalkSprite = loadImage("Sprite/character_walk.gif");
ninjastar1move = loadImage("Sprite/NinjaStar2x0.gif");
}
function setup() {
createCanvas(400, 400);
ninjastar = Ninjastar();
}
function draw() {
background(220);
player.move();
player.display();
ninjastar.move();
ninjastar.display();
}
function Player() {
let x = 200;
let y = 200;
let animationState = 0;
function move() {
animationState = 0;
if (keyIsDown(RIGHT_ARROW)) {
x = x + 5;
animationState = 1; //walk animation
}
if (keyIsDown(LEFT_ARROW)) {
x = x - 5;
animationState = 1; //walk animation
}
if (keyIsDown(UP_ARROW)) {
y = y - 5;
animationState = 1;
}
if (keyIsDown(DOWN_ARROW)) {
y = y + 5;
animationState = 1;
}
}
function display() {
if (animationState == 0) {
image(charIdleSprite, x, y);
} else if (animationState == 1) {
image(charWalkSprite, x, y);
}
}
return { move, display };
}
function Ninjastar() {
let x = random(0, 500);
let y = random(0, 500);
let animationState = 0;
function move() {
x = x + 7;
if (x > width) {
x = 0;
}
y = y + 5;
if (y > width) {
y = 0;
}
}
function display() {
if (animationState == 0) {
image(ninjastar1move, x, y);
}
}
return { move, display };
}