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

2018/5/7

Actors Spawning on Each Other

Ectomorph Ectomorph

2018/5/7

#
I have created a multiple choice answer shooter game, where the answer actors spawn in a random row but they occasionally spawn on top of each other concealing an answer underneath. I have read various discussions on this but can't work out how to make it work in my code. I have included the code below. Any help would be grateful. public void addObjectsToWorld() { getWorld().addObject(new Answer(folderPath + "/correct.png", true), Greenfoot.getRandomNumber(getWorld().getWidth()), 150); getWorld().addObject(new Answer(folderPath + "/false1.png", false), Greenfoot.getRandomNumber(getWorld().getWidth()), 150); getWorld().addObject(new Answer(folderPath + "/false2.png", false), Greenfoot.getRandomNumber(getWorld().getWidth()), 150); getWorld().addObject(new Question(folderPath + "/question.png"),getWorld().getWidth() - getWorld().getWidth()+ 450, 70); }
danpost danpost

2018/5/9

#
You will need to use exact coordinates for your answers. You can do that by listing and shuffling either the coordinates or the answers. For example, to shuffle the coordinates:
public void addObjectsToWorld(){
    int gap = getWorld().getWidth()/3; // spacing
    Integer[] xs = { gap/2, gap*3/2, gap*5/2 }; // coordinates
    java.util.Collections.shuffle(java.util.Arrays.asList(xs)); // shuffle
    getWorld().addObject(new Answer(folderPath+"/correct.png", true), xs[0], 150);
    getWorld().addObject(new Answer(folderPath+"/false1.png", false), xs[1], 150);
    getWorld().addObject(new Answer(folderPath+"/false2.png", false), xs[2], 150);
    getWorld().addObject(new Question(folderPath+"/question.png"),getWorld().getWidth()/2, 70);
}
Ectomorph Ectomorph

2018/5/9

#
Thank you that, I'll give it a go.
You need to login to post a reply.