We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › My minions... where are they (ArrayList question)
Page Index Toggle Pages: 1
My minions... where are they? (ArrayList question) (Read 951 times)
My minions... where are they? (ArrayList question)
Dec 11th, 2009, 12:30pm
 
At line 126, I've used an ArrayList in order to spam minions (as in "enemies.add").
Code:

int screenSize, arraySize,readScore;
int[] timer;
int[] score;

boolean startGame;
boolean pauseGame;
boolean endGame;

SPACE zone;
AVATAR player;
MINION enemy;
ArrayList enemies;

void setup()
{
 screenSize = 400;
 size (screenSize,screenSize);
 frameRate(30);
 
 arraySize = 4;
 timer = new int [arraySize];
 score = new int [arraySize];
 readScore = arraySize - 1;
 
 startGame = false;
 pauseGame = false;
 endGame = false;
 
 zone = new SPACE
 (
 loadImage("figura1.png"), loadImage("figura2.png"),
 loadImage("figura3.png"), color(27,214,219), (screenSize/2),
 (screenSize/2), (screenSize/15)
 );
 
 player = new AVATAR
 (
 color(31,155,16), (screenSize/2), (screenSize/2),
 (screenSize/15),(screenSize/80)
 );
 
 enemies = new ArrayList();
}

void mousePressed()
{
 startGame =! startGame;
 pauseGame = true;
 
 if(endGame == true)
 {
   endGame = false;

   for(int i = 0; i < timer.length; i++)
   {
timer[i] = 0;
   }
 }
}

void keyPressed()
{
 if(key == 't')
 {
   endGame =! endGame;
 }
}

void draw()
{
 background(color(#141B5D));
 smooth();
 zone.display();
 
 if (endGame == false)
 {
   player.avatarSize = screenSize/15;
   player.display();
   
   if (startGame == true)
   {
zone.alphaTest = true;
player.move();

//TIMER
if (pauseGame == false)
{
 timer[0]++;
 if (timer[0] > 9)
 {
   timer[1]++;
   timer[0] = 0;
 
   if(timer [1] > 9)
   {
timer[2]++;
timer[1] = 0;
   
if(timer[2] > 9)
{
 timer[3]++;
 timer[2] = 0;

 if(timer[3] > 9)
 {
   for(int i = 0; i < timer.length; i++)
   {
timer [i] = 9;
   }
 }
}
   }
 }
}
   
if(
timer [2] == 1 && timer[3] == 0 ||
timer [2] == 2 && timer[3] == 0 ||
timer [2] == 3 && timer[3] == 0 ||
timer [2] == 4 && timer[3] == 0 ||
timer [2] == 5 && timer[3] == 0 ||
timer [2] == 6 && timer[3] == 0 ||
timer [2] == 7 && timer[3] == 0 ||
timer [2] == 8 && timer[3] == 0)
{
 enemies.add (new MINION(color(193,12,12), player.xpos,
 player.ypos, player.avatarSize, (screenSize/15),
 (screenSize/80)));
}

for(int i = enemies.size()-1; i >= 0; i--)
{
 MINION enemy = (MINION) enemies.get(i);
 enemy.display();
 enemy.move();
}
   }

   if(pauseGame == true && startGame == false)
   {
zone.pause();
   }
 }
 
 if(endGame == true)
 {
   startGame = false;
   pauseGame = false;    
   player.end();
   zone.end();
   score = reverse(timer);
   
   for(int i = enemies.size()-1; i >= 0; i--)
   {
enemies.remove(i);
   }
 }
}


The game runs, but the minios never appear... Sad

Here is the MINION class:
Code:

public class MINION
{
 color c;
 public float xspam, yspam, xSpamDirection,ySpamDirection,
 xpos,ypos;
 public int playerXpos,playerYpos,playerSize,minionSize,
 xspeed,yspeed;
 public boolean spam;
 
 public MINION(
 color writeColor, int writePlayerXpos, int writePlayerYpos,
 int writePlayerSize, int writeMinionSize, int writeSpeed)
 {
   c = writeColor;
   playerXpos = writePlayerXpos;
   playerYpos = writePlayerYpos;
   playerSize = writePlayerSize;
   minionSize = writeMinionSize;
   xspeed = writeSpeed;
   yspeed = writeSpeed;
   
   xspam = random (0,1);
   yspam = random (0,1);
   xSpamDirection = random (0,1);
   ySpamDirection = random (0,1);
   spam = true;
 }
 
 public void display()
 {
   if(spam == true)
   {
if(xspam == 0)
{
 xpos = random((((height + width)/ 2) / 15), (playerXpos - playerSize));
}

else
{
 xpos = random((playerXpos + playerSize),(((height + width)/ 2) - (((height + width)/ 2)/15)));
}

if(yspam == 0)
{
 ypos = random((((height + width)/ 2)/15), (playerYpos - playerSize));
}

else
{
 ypos = random((playerYpos + playerSize), (((height + width)/ 2) - (((height + width)/ 2)/15)));
}

spam = false;
   }

   noFill();
   strokeWeight(((width + height) /2) /80);
   stroke(c);
   rectMode(CENTER);
   rect(xpos, ypos, minionSize, minionSize);
 }
 
 public void move()
 {
   xpos = xpos + xspeed;
   ypos = ypos + yspeed;

   if(spam == true)
   {
xspeed = xspeed * (xSpamDirection  == 0? 1 : -1);
yspeed = yspeed * (ySpamDirection  == 0? 1 : -1);
   }

   if(xpos >= (width - (minionSize))
|| xpos <= (0 + (minionSize)))
   {
xspeed = xspeed * (-1);
   }

   if(ypos >= (height - (minionSize))
|| ypos <= (0 + (minionSize)))
   {
yspeed = yspeed * (-1);
   }
 }
}

Help?
Re: My minions... where are they? (ArrayList question)
Reply #1 - Dec 11th, 2009, 2:48pm
 
There's a lot going on there to debug without having all the code and trying to run it...  And no that's not a suggestion for you to post all the code Cheesy

A few avenues to explore:

1.  Are the values used to add a MINION what you expect them to be?  Use println() to display variables as necessary.  (In fact use println() a lot - if you're not convinced a method/condition etc. is being executed stick a println() in there to be sure Wink )

2.  println(enemies.size()): make sure the ArrayList actually contains minions...

3.  Check that it's not a problem with your MINION.display() method.  Perhaps check the value of minionSize; give it a fill() colour just in case the formula you use for strokeWeight is giving unexpected non-renderable results...

4.  Check your minions actually remain onscreen: your boundary test is suspect.  Multiplying xspeed by -1 is not enough.  Consider this: your MINION is travelling at sufficient speed to removed far off screen and another factor then reduces its speed.  Speed is multiplied by -1; but now it's not enough to get it back on screen; so in the next frame it is again multiplied by -1, sending it away from the screen again...  So, it's usual to place the object back within the bounds before multiplying by -1 (which means separate tests for left/right/top/bottom).

That's plenty to get you started...  Good luck!

<snip />

Edited:
Forget my 'unrelated note' - I was being dense there for a moment
Re: My minions... where are they? (ArrayList question)
Reply #2 - Dec 12th, 2009, 2:47am
 
would be easier if we had the whole code so we can run it rather than just reading it (is missing SPACE and AVATAR classes and the images)

(oops, i should've read blindfish's post as i've just contradicted him completely...)

in the meanwhile this:
Code:

 timer[0]++;
 if (timer[0] > 9)
 {
   timer[1]++;
   timer[0] = 0;
 
   if(timer [1] > 9)
   {
timer[2]++;
timer[1] = 0;
   
if(timer[2] > 9)
{
 timer[3]++;
 timer[2] = 0;

 if(timer[3] > 9)
 {
   for(int i = 0; i < timer.length; i++)
   {
timer [i] = 9;
   }
 }
}
   }
 }
}
   
if(
timer [2] == 1 && timer[3] == 0 ||
timer [2] == 2 && timer[3] == 0 ||
timer [2] == 3 && timer[3] == 0 ||
timer [2] == 4 && timer[3] == 0 ||
timer [2] == 5 && timer[3] == 0 ||
timer [2] == 6 && timer[3] == 0 ||
timer [2] == 7 && timer[3] == 0 ||
timer [2] == 8 && timer[3] == 0)


is terrible. you appear to be adding things when the timer = 10, 20, 30,... 80. you can do this easily enough without jumping through the hoops you have updating each digit of the timer individually - use the modulo (%) operator.

timer++;
if ((timer % 10) == 0) && (timer / 10 < 9)) {
 // add enemy
}

timer % 10 = 0 when the last digit is 0
timer / 10 < 9 when timer < 90. put them together and it's true on 10, 20, 30, 40, 50, 60, 70 and 80 (oh, and 0)
Re: My minions... where are they? (ArrayList question)
Reply #3 - Dec 12th, 2009, 2:57am
 
you have xspam (and yspam) as a float

you then xspam = random(0, 1);

then
if (xspam == 0) {
} else {
}

the chances of xspam being 0 is minimal so it's most probably doing the other branch. i'd look at the valus of xpos that's being generated there as the maths isn't immediately obvious.

also

xpos = random((playerXpos + playerSize),(((height + width)/ 2) - (((height + width)/ 2)/15)));

i *think* i remember a bug where that's expecting the first clause to be smaller than the second clause and does something odd if it isn't

ie random(2, 1) doesn't do what random(1, 2) does.
Re: My minions... where are they? (ArrayList question)
Reply #4 - Dec 13th, 2009, 7:04am
 
Thanks everyone for all you advices, its now working as planned. Now there's a minor issue: my minions flicker a little, any ideal why?
Page Index Toggle Pages: 1