i have some questions.
1)Why do you not need to declare the width and height of the character is it because it is constant through out? 2)why is there a rect() in the player class?
3)what does (dist(x,y,width/2,height/2)<100 mean ?
it checks if the object is less than 100 pix from the middle
but you must see the whole context:
x = random(width);
y = random(height);
while (dist(x, y, width/2, height/2) < 100) {
x = random(width);
y = random(height);
}
first you make the objects x,y in the whole window / canvas
then check and try again until it is not in the middle!
-
Pretty much, yes. I could put the characterâs size in the player class, and then use it when I draw the player. This might be useful if, for example, you planned on having your playerâs size change during the game. I didnât. But I did think that it might be useful to have in in the Foe class, because enemies are probably a lot more likely to come in different sizes.
-
That rect() is what draws the player! Thatâs what the blue square is!
-
Yes, as @kll said, that is to make sure there is a âholeâ in the middle where there are no enemies. That way the player doesnât die immediately (assuming you add player-dies-if-touching-enemy logic later).
What is this boolean draw? why do you not need to draw the enemy rectangle in this? or in my case instead of rect i will be using image()
That is the draw method for the Bullet class. It draws one Bullet. You are aware that the Blue square can shoot bullets, right? Click in the sketch. A bullet is created at the blue player square that is moving towards the mouseâs position.
The Bulletâs draw() function returns a boolean value so that the loop in the main draw() function can remove Bullets that are not alive from the ArrayList of bullets. Basically, the Bullet reports if it is still alive via the return value of its draw() function. If it is not alive any more, it is removed from the ArrayList.
does the Player player replace the Player players[] = new Player[1] ? since there is technically only one player so no need for an array?
Yes. There is only one Player. It is the Player called player
. There is no need for an array of Player objects if there is only one of them!
my original one is using
Player players = new Player[1];
PImage chara = new PImage[1];
when i try to introduce the bullet class as above, its says
players.charaStartX cannot be resolved or is not a field.
this is around the part where
Bullet()
{
alive = true;
x = player.x; ( i use float charaStartX in Player class so i wrote x = players.charaStartX)
}
i am also trying to use Player players but since i was using array to load images earlier i m not to sure how to convert these:
for (int i=0; i<players.length; i++)
{
players[i] = new Player();
}
// from void setup()
and
// from void Homepage()
for (int i=0; i<players.length; i++)
{
players[i].draw(300,500,300,300,i);
}
and basically i know how to draw a normal character using image. but now i m using classes i m not too sure do i just call the class to draw the image or i can just use âimage(âŚ)â
this is somewhere where i am at now
https://drive.google.com/file/d/1QMmwwGcCObS7C72scbZr6o2bWcekZOR2/view?usp=sharing
i m trying not to copy everything since i feel that it isnt right. i m retaining some parts of what i did on my own before but i copied the classes thing (not much choice there)
Letâs be clear: You no longer want to have an array of Players. You only need one Player, so itâs not an array called players, itâs now one Player called player
.
As such, you should first remove any loops that are happening over the players array. Because there is no players array any more.
To make the new Player to store in your Player object called player
, just do:
Player player = new Player();
That is, there is a Player object. It is called player
. And the value assigned to it is a new Player object.
Similarly, there is no need for a loop over the players array in draw()
, once again, because there is only one Player object. Just draw it directly:
player.draw();
Notice that the player class doesnât have a charaStartX
variable! The name is simpler now. Itâs just x
.
You still want to load the images for the ghosts and the art of chara in the same way:
PImage chara; // There is one image called chara.
PImage[3] ghosts; // There is also an array of three images for the ghosts.
// Load the images the same way as before. One directly, the other three in a loop.
In the player class, you can just call image()
to draw the image of chara instead of drawing a blue square with rect()
. Notice that the PImage called chara
is a global variable, which means that you can access it inside any class or function.
In short:
image( chara, x, y );
if i get rid of charaStartX in player class what happens if i need to refer to it outside of the class? i use players.x? when i need to players.draw(charaStartX, charaStartY, charaWid, charaHght) it says i cant use players.x inside of it. and when i put x it says component not visible.