Assignment is due tomorrow afternoon. What I need to do is make it so the 2 animations are not aloud to go past the edges of the screen. I'm not necessarily asking for the answer but just need guidance in the right direction.
Would an 'if' statement be the easiest way to keep the animations from going past the edges? And would I put it inside the keyPressed or draw function?
Heres the code: (forgive me im noob)
Code
//superhero variables
var xPos = 250;
var yPos = 200;
var x_one = 210;
var y_one = 230;
var x_two = 250;
var y_two = 200;
var x_three = 290;
var y_three = 230;
//villain variables
var x_eartwo = 51;
var x_ear = 17;
var x_mouthtwo = 47;
var x_mouth = 21;
var x_eyetwo = 42;
var x_eye = 29;
var x_Three = 34;
var x_Two = 66;
var x_One = 5;
var x = 25;
var y = 176;
var ytwo = 155;
var ythree = 133;
var yfour = 149;
var yfive = 147;
var ysix = 165;
var ysev = 174;
var superhero = function() {
background(0, 128, 255);
//grass
noStroke();
fill(25, 204, 61);
rect(0, 300, 410, 300);
//cape
fill(255, 0, 0);
triangle(x_one, y_one, x_two, y_two, x_three, y_three);
//ghost hero
fill(20, 18, 18);
ellipse(xPos, yPos, 30, 30);
};
//////////villain/////////////
var villain = function() {
//appendage
fill(255, 0, 0);
stroke(0, 89, 255);
rect(x,ysev,18,85);
//head
triangle(x_One,y,x_Two,y,x_Three,ythree);
//eyes
stroke(13, 28, 4);
fill(145, 127, 47);
ellipse(x_eye,ytwo,7,7);
ellipse(x_eyetwo,ytwo,7,7);
//mouth
noFill();
line(x_mouth,ysix,x_mouthtwo,ysix);
//ears
fill(179, 16, 16);
ellipse(x_ear,yfour,7,12);
ellipse(x_eartwo,yfive,7,12);
};
keyPressed = function() {
//superhero//
if (keyCode === LEFT) {
xPos = xPos - 10;
x_one = x_one - 10;
x_two = x_two - 10;
x_three = x_three - 10;
}
else if (keyCode === RIGHT) {
xPos = xPos + 10;
x_one = x_one + 10;
x_two = x_two + 10;
x_three = x_three + 10;
}
else if (keyCode === UP) {
yPos = yPos - 10;
y_one = y_one - 10;
y_two = y_two - 10;
y_three = y_three - 10;
}
else if (keyCode === DOWN) {
yPos = yPos + 10;
y_one = y_one + 10;
y_two = y_two + 10;
y_three = y_three + 10;
}
//villain//
else if (keyCode === 65) { //move left. Letter "A"
x = x - 10;
x_One = x_One - 10;
x_Two = x_Two - 10;
x_Three = x_Three - 10;
x_eye = x_eye - 10;
x_eyetwo = x_eyetwo - 10;
x_mouth = x_mouth - 10;
x_mouthtwo = x_mouthtwo - 10;
x_ear = x_ear - 10;
x_eartwo = x_eartwo - 10;
}
else if (keyCode === 68) { //move right. Letter "D"
x = x + 10;
x_One = x_One + 10;
x_Two = x_Two + 10;
x_Three = x_Three + 10;
x_eye = x_eye + 10;
x_eyetwo = x_eyetwo + 10;
x_mouth = x_mouth + 10;
x_mouthtwo = x_mouthtwo + 10;
x_ear = x_ear + 10;
x_eartwo = x_eartwo + 10;
}
else if (keyCode === 83) { //move down. Letter "S"
ysev = ysev + 10;
y = y + 10;
ythree = ythree + 10;
ytwo = ytwo + 10;
ysix = ysix + 10;
yfour = yfour + 10;
yfive = yfive + 10;
}
else if (keyCode === 87) { //move up. Letter "W"
ysev = ysev - 10;
y = y - 10;
ythree = ythree - 10;
ytwo = ytwo - 10;
ysix = ysix - 10;
yfour = yfour - 10;
yfive = yfive - 10;
}
};
draw = function() {
superhero();
villain();
};
This post was edited by Shakti on Oct 11 2016 11:24pm