在p5.js中交换变量

时间:2016-10-03 22:41:56

标签: processing p5.js

我在程序中定义了一个名为fighter的变量。我有一个if keyPressed函数,它根据键选择一个特定的图像。我遇到的问题是图像交换但重叠原始图像。我还尝试在战斗机的原始位置添加一个else语句,但仍然是相同的响应。

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);
        } 
    };

};

1 个答案:

答案 0 :(得分:1)

由于某种原因,您将fighter设置为等于image()函数返回的值。这没有多大意义。

相反,我认为您要将fighter设置为等于其中一个图像,然后将fighter传递给image()函数。像这样:

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);