How to switch an Image from arguments
in
Programming Questions
•
10 months ago
I have several enemy sprites I know how to switch their location and speed but not how to change the sprite if anybody could lend me a hand, I have about 12 enemies i wanna use where it says idk how to turn this into a varible
Enemy(float tempXpos, float tempYpos, float tempXspeed) {
Enemy_1 = loadImage("Enemy_1.png");
where i can change what I'm loading for an image passing it threw an argument.
Enemy(float tempXpos, float tempYpos, float tempXspeed) {
Enemy_1 = loadImage("Enemy_1.png");
where i can change what I'm loading for an image passing it threw an argument.
- //Global Varibles
Enemy myEnemy; //Enemey object Oriented varible
Enemy myEnemy1; //Second Enemy Object Variable
//--Global Variable end--
void setup(){ //basic setup for enemy 3 test
size(1000,680); //skect size
smooth(); //does something i forgot
myEnemy = new Enemy(200,400,5);// initializin enemy varible
myEnemy1 = new Enemy(300,100,4); //initializing second varible of enemy
}
void draw(){ //our main program
background(0);
myEnemy.drawEnemy();
myEnemy.moveEnemy();
myEnemy.wallCheck();
myEnemy1.drawEnemy();
myEnemy1.moveEnemy();
myEnemy1.wallCheck();
}
class Enemy{
PImage Enemy_1;
PImage Enemy_2;
PImage Enemy_3;
PImage Enemy_4;
PImage Enemy_5;
PImage Enemy_6;
PImage Enemy_7;
PImage Enemy_8;
PImage Enemy_9;
PImage Enemy_10;
PImage Enemy_11;
PImage Enemy_12;
PImage Enemy_13;
float enemyX;
float enemyY;
float speed;
Enemy(float tempXpos, float tempYpos, float tempXspeed) {
Enemy_1 = loadImage("Enemy_1.png");
enemyX = tempXpos;
enemyY = tempYpos;
speed = tempXspeed;
}
void drawEnemy(){
image(Enemy_1,enemyX,enemyY);
}
void moveEnemy(){
// Change the x location by speed
enemyX = enemyX + speed;
}
void wallCheck(){
if ((enemyX > width - 170) || (enemyX < 0)){
speed = speed * -1;
}
}
}
1