This site requires JavaScript, please enable it in your browser!
Greenfoot back
Vigelandzoon
Vigelandzoon wrote ...

2014/1/25

i made a array for my actors but they wont stay in the position i want how can i do this

1
2
Vigelandzoon Vigelandzoon

2014/1/25

#


Kraanmachinist[]kraanmachinisten = new Kraanmachinist[2];
        for(int i =0; i<kraanmachinisten.length;i++)
        {
          kraanmachinisten[i] = new Kraanmachinist();
          int kraanmachinistX= Greenfoot.getRandomNumber(getWidth()-100)+50;
          int kraanmachinistY= Greenfoot.getRandomNumber(getHeight()-100)+50;
          addObject(kraanmachinisten[i],kraanmachinistX,kraanmachinistY);
        }


Vigelandzoon Vigelandzoon

2014/1/25

#
its a sort of memory game
Vigelandzoon Vigelandzoon

2014/1/25

#
they pop randomly everywhere
bourne bourne

2014/1/26

#
You do know getRandomNumber returns random numbers? But maybe you mean that they are jumping around? Which case I suppose you are calling this code more than once (like within an act method).
Vigelandzoon Vigelandzoon

2014/1/26

#
they pop up randomly that is good but i want to put them for example in certain specifiek positons like 50x 50y , 100x 50y ect
danpost danpost

2014/1/26

#
Vigelandzoon wrote...
they pop up randomly that is good but i want to put them for example in certain specifiek positons like 50x 50y , 100x 50y ect
Then you need to adjust the formulae for x and y. The number of places you can then spawn is however many times 50 goes into the width and height. That is your range of random numbers.
int x = Greenfoot.getRandomNumber(getWidth()/50)*50;
int y = Greenfoot.getRandomNumber(getHeight()/50)*50;
danpost danpost

2014/1/26

#
Then you may want to remove (0, n) and (n, 0) locations from this:
int x = (Greenfoot.getRandomNumber(getWidth()/50-1)+1)*50;
int y = (Greenfoot.getRandomNumber(getHeight()/50-1)+1)*50;
This subtracts one inside the random get because we are removing one more possibility in both directions. Then, it add one outside the random get to start at one times 50, or 50, both ways (the initial offset). This could have been written this way:
int x = 50+Greenfoot.getRandomNumber(getWidth()/50-1)*50;
int y = 50+Greenfoot.getRandomNumber(getHeight()/50-1)*50;
Vigelandzoon Vigelandzoon

2014/1/26

#
my world is 400 x 500 y but somtimes they wont stay in it
danpost danpost

2014/1/26

#
Are they being place at the proper locations otherwise? where are they being placed when 'they wont stay in it'? The size of your world should not matter (provided that it is at least 100x100).
Vigelandzoon Vigelandzoon

2014/1/26

#
danpost danpost

2014/1/26

#
What are the size of the images that are being placed randomly?
Vigelandzoon Vigelandzoon

2014/1/26

#
100 px
danpost danpost

2014/1/26

#
What code do you have in the Kraanmachinist class?
Vigelandzoon Vigelandzoon

2014/1/26

#
public void act() 
{
        kaart();
        changeandremove();
}    
    
public void kaart()
{
  setImage("150px_RGB_WEBSAFE_D_B_G_26-29652.gif");
}
  
public void changeandremove()
{
   if(Greenfoot.mouseClicked(this)) 
    {
        World myWorld = getWorld();  
        setImage("Arbocatalogus_beton_Beroep_Kraanmachinist.jpg");
        Greenfoot.delay(110);
        getWorld().removeObject(this);
        Greenfoot.playSound("Air Plane Ding-SoundBible.com-496729130.wav");
        Background background = (Background)myWorld;
        Counter counter = background.getCounter();
        counter.addScore();
        
    }
}
danpost danpost

2014/1/26

#
The image of the actor does not need to be set every act cycle. Move line 3 ('kaart();') to the constructor of the class:
public Kraanmachinist()
{
    // any code you may already have here
    kaart();
}
This will help in not over-taxing your CPU, but not in the issue of your images being placed in unwanted positions. Re-compile your project and run it to get your images in your world. Stop the scenario (click on 'Pause' button) and for each image, right click on an image and click 'Inspect' and report back what the int x and int y values are.
There are more replies on the next page.
1
2