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

2017/5/15

Randomly moving object from right to left

csfail2002 csfail2002

2017/5/15

#
I am working on a project and i can not get the code correctly for a cloud to move across the screen from right to left all starting at 623 on the x axis on a 600 by 400 screen while starting from random heights. It does not interfere with any objects, merely for background purposes. It disappears when it hits 0 on the x axis. For my code the amount of clouds appearing keeps increasing rapidly when i only want no less than about ten clouds on the screen but no more than 17. Please help ty.
Yehuda Yehuda

2017/5/15

#
What's not happening as it should? Show the code (full class copy - Ctrl+A Ctrl+C) that's supposed to take care of what you want to do.
danpost danpost

2017/5/15

#
First, spawn the clouds from your world class act method (add one if not already included in the class). Then if you have issues, post the code you tried and ask for assistance.
csfail2002 csfail2002

2017/5/15

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.Random; import java.util.List; /** * Write a description of class Cloud here. * * @author (your name) * @version (a version number or a date) */ public class Cloud extends ObjectActor { private static int speed; private Random randomizer = new Random(); private int frequency = 0; public Cloud (int tSpeed) { GreenfootImage elephant = new GreenfootImage(getImage()); elephant.scale(100,60); setImage (elephant); speed=(tSpeed); } public void act() { setLocation(getX()-speed,getY()); if (getX()==0) { getWorld().removeObject(this); } spawn(); } public void spawnRemovers() { int x = 620; int y = randomizer.nextInt(getWorld().getHeight()); // int y = 200; //int y = getWorld().getHeight(); getWorld().addObject(new Cloud(2), x, y); } public void spawn() { int randomInt = randomizer.nextInt(100); if( frequency > 10) { frequency = 0; spawnRemovers(); } frequency++; } }
csfail2002 csfail2002

2017/5/15

#
Could you explain more?
danpost danpost

2017/5/15

#
An idea I had involves placing all 17 clouds in the world to begin with and uses the transparency of the image of the cloud to temporarily hide a cloud for a short time when it reaches the left edge of the world; then showing back up on the right when its timer has run its course. Create all 17 clouds from in the world constructor. Spawn the clouds into the world with a random x value of:
1
int x =  randomizer.nextInt(getWorld().getWidth()+150)-150;
This will cause a few to be spawned along the left edge. Have each Cloud object act once after being placed into the world so that any along that edge will hide and have their timers reset. An alternative to acting once is using the 'addedToWorld' method to check at left edge and hiding and setting the timer. In the Cloud act method, when x location of a cloud is zero, do not remove the cloud from the world, but set the transparency of its image to zero and set an int timer field to some nominal random value (maybe between zero and 45 -- will probably need adjusting) to reappear (as if respawning) at the right edge in. Have the act method also decrement the timer and reset the transparency of the image of the cloud back to 255 and relocate the cloud at the right edge when it (the timer) reaches zero.
You need to login to post a reply.