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

2020/4/27

how do i make objects spawn in random places

1
2
ninjapuffin ninjapuffin

2020/4/27

#
Im having trouble making bombs spawn in random places does anyone have any ideas
danpost danpost

2020/4/27

#
ninjapuffin wrote...
Im having trouble making bombs spawn in random places does anyone have any ideas
Do you know how to spawn a bomb at all? What code would you use? What do you think needs to change? How do you think it needs to be changed?
Kostya20052011 Kostya20052011

2020/4/27

#
x=Greenfoot.getRandomNumber(getWorld().getWidth()); y=Greenfoot.getRandomNumber(getWorld().getHeight()); Bomb bm=new Bomb(); addObject(bm,x,y);
ninjapuffin ninjapuffin

2020/4/27

#
well my code isn't working ill test out kostya20052001s code
ninjapuffin ninjapuffin

2020/4/27

#
his code didn't work but this is my code if (Greenfoot.getRandomNumber(100) < 60) { Bomb Bomb = new Bomb(); addObject(Bomb,getX(), getY()); } add object is not compiling
danpost danpost

2020/4/27

#
Replace your 2 inner lines with what Kostya20052011 wrote -- only, add "int " to the beginning of his first 2 lines (and remove all "getWorld()."s).
ninjapuffin ninjapuffin

2020/4/27

#
this code just dosent seem to work X=Greenfoot.getRandomNumber; Y=Greenfoot.getRandomNumber; Bomb bm=new Bomb(); addObject(bm,X,Y)
danpost danpost

2020/4/28

#
ninjapuffin wrote...
this code just dosent seem to work << Code Omitted >>
Go back to my last post and follow directions properly.
ninjapuffin ninjapuffin

2020/4/28

#
is this what you mean public void randomSpawn() { if (Greenfoot.getRandomNumber(100) < 60) { int x=Greenfoot.getRandomNumber(getWidth()); int y=Greenfoot.getRandomNumber(getHeight()); Bomb bm=new Bomb(); addObject(bm,x,y); }
danpost danpost

2020/4/28

#
ninjapuffin wrote...
is this what you mean << Code Omitted >>
Does it work?
Kostya20052011 Kostya20052011

2020/4/28

#
First, it must be written in the code of the world, and second, x,y are variables, they must be declared! x=Greenfoot.getRandomNumber(getWorld().getWidth()); y=Greenfoot.getRandomNumber(getWorld().getHeight()); Bomb bm=new Bomb(); addObject(bm,x,y);
Kostya20052011 Kostya20052011

2020/4/28

#
And together with Bomb, you must write the name of the object that you have
ninjapuffin ninjapuffin

2020/4/28

#
it didn't work danpost
danpost danpost

2020/4/28

#
ninjapuffin wrote...
it didn't work danpost
Is the method being called to execute (from act)?
Kostya20052011 Kostya20052011

2020/4/28

#
public void act(){ randomSpawn(); } public void randomSpawn() { if (Greenfoot.getRandomNumber(100) < 60) { int x=Greenfoot.getRandomNumber(getWidth()); int y=Greenfoot.getRandomNumber(getHeight()); Bomb bm=new Bomb(); addObject(bm,x,y); } }
There are more replies on the next page.
1
2