Reputation: 179
I have defined a variable called fighter in my program. I have an if keyPressed function which selects a specific image based on key. The problem I am having is that that images are swapping but overlapping the original image. I also attempted to add an else statement with the fighter in the original position but still same response.
var fighter;
var stance;
var kick;
var jab;
var cross;
var mx; //Use to constrain fighter to center of circle
var my;
function preload(){
stance = loadImage("img/stance.svg");
kick = loadImage("img/kick.svg");
jab = loadImage("img/jab.svg");
cross = loadImage("img/cross.svg");
};
function setup(){
createCanvas(1280,720);
};
function draw(){
background(0, 246, 255);
fill("red");
ellipse(width/2,height/2,500,500);
mx = constrain(mouseX,width/2-250,width/2+250);
my = constrain(mouseY,height/2-250,height/2+250);
fighter = image(stance,mx,my);
if(keyIsPressed){
if((key == "a" || key == "A")){
fighter = "";
fighter = image(jab,mx,my);
}
else if ((key == "w" || key == "W")) {
fighter = image(cross,mx,my);
}
else if ((key == "s" || key == "S")) {
fighter = image(kick,mx,my);
}
};
};
Upvotes: 1
Views: 437
Reputation: 42176
You're setting fighter
equal to the value returned by the image()
function for some reason. This doesn't make a ton of sense.
Instead, I think you want to set fighter
equal to one of the images, and then pass fighter
into the image()
function. Something like this:
fighter = stance;
if(keyIsPressed){
if((key == "a" || key == "A")){
fighter = jab;
}
else if ((key == "w" || key == "W")) {
fighter = cross
}
else if ((key == "s" || key == "S")) {
fighter = kick;
}
}
image(fighter, mx,my);
Upvotes: 1