# Object Speed problem! (move();)

• January 13th, 2013, 03:02 PM
Optimizer
Object Speed problem! (move();)
Hey guys! I need some help with my 'Auto(car)' that needs to go slower.. right now it's moving too fast. this is my code:

Code :

``` public void act() { world theWorld = (world) getWorld(); if(!theWorld.isPaused) { move(1); setAutoImage(); } }   public void setAutoImage() { GreenfootImage autoimg = new GreenfootImage("Auto.png"); if(getX() >= (getWorld().getWidth()-3)) { getWorld().addObject(new Auto(), 0, 60); getWorld().removeObject(this); } setImage(autoimg); } }```

As you can see i have move(1);

It cant go any slower.. help please!
• January 13th, 2013, 03:11 PM
Norm
Re: Object Speed problem! (move();)
Can you make a small complete program the compiles, executes and shows the problem?

Quote:

It cant go any slower.
If 1 represents how far to move in a time span, where is the length of the time span set?
• January 13th, 2013, 03:23 PM
Optimizer
Re: Object Speed problem! (move();)
ehm.. idk how to do that.. i'll try to explain as good as possible..

There is this car in my game that moves straight in 1 line. (addObject(new Auto(), x, x))
The actor (Auto) has the code I showed in my first post.
I had it set to move(5); which made the car drive ridiculously fast.
I've set it to move(1); but the car is still moving too fast.
I tried move(0.1); but that didn't work obviously..

--- Update ---

ehm.. idk how to do that.. i'll try to explain as good as possible..

There is this car in my game that moves straight in 1 line. (addObject(new Auto(), x, x))
The actor (Auto) has the code I showed in my first post.
I had it set to move(5); which made the car drive ridiculously fast.
I've set it to move(1); but the car is still moving too fast.
I tried move(0.1); but that didn't work obviously..

--- Update ---

I've got this in the world to balance the game:

Code :

``` public void act() {   if(!isPaused) { setScore(); Greenfoot.setSpeed(40); } pauseGame(); started(); }```

so with move(1);
and Greenfoot.setSpeed(40); (of the world)

it drives too fast.
• January 13th, 2013, 03:25 PM
Norm
Re: Object Speed problem! (move();)
If the car is being moved every 20 millisec and you change it to be moved every 40 millisecs it should move half as fast. If you change the time between moves to 1000 millisecs, it should move very slowly.
• January 13th, 2013, 03:28 PM
Optimizer
Re: Object Speed problem! (move();)
I dont know how to do that:/ im new to programming
• January 13th, 2013, 03:32 PM
Norm
Re: Object Speed problem! (move();)
Can you post the code that you are using? You need to post enough code to compile, execute and test with that shows the problem?

Who wrote the code? Can you ask them how to change it so it works the way you want it to?
• January 13th, 2013, 03:34 PM
Optimizer
Re: Object Speed problem! (move();)
I made it with a projectgroup.. here is the code for the game world and the code for the car:

Code :

```import greenfoot.*; //import javax.swing.*; import java.util.ArrayList;   public class world extends World { public boolean readPause = true; public boolean isPaused = false; Resume resume = new Resume(); ArrayList<ArrayList<Water>> waters = new ArrayList<ArrayList<Water>>(); private int score = 100; Player player = new Player(); private int gameMode2 = 0; private GreenfootSound music = new GreenfootSound("Music.mp3"); private ScoreReader scorereader = new ScoreReader();   public int getGameMode2() { return gameMode2; }   public void setGameMode2(int gameMode) { gameMode2 = gameMode; }   public int getScore() { return score; }   public void setScore(int score) { this.score += score; }   public world() { super(80, 80, 10);     int gameMode = scorereader.readHighscore(5);// gameMode opzoeken en opslaan   setGameMode2(gameMode);   WaterPrepare[] watersprepare = new WaterPrepare[3600];// array voor waterprepareobjecten aanmaken Dijk[] dijken = new Dijk[1200];//array voor dijkobjecten aanmaken Binnendijk[] binnendijken = new Binnendijk[2800];//array voor binnendijk objecten aanmaken ArrayList<Zandzak> zandzakken = new ArrayList<Zandzak>();   int a=0;//counter aanmaken om vorige waterprepare object te onthouden int b=0;//counter aanmaken om vorige dijk object te onthouden int c=0;//counter aanmaken om vorige binnendijk object te onthouden int f=0;//counter voor zandzakken   for(int z=0; z<watersprepare.length; z++)//array voor waterobjecten vullen { watersprepare[z] = new WaterPrepare(); }   for(int z=0; z<dijken.length; z++)//array voor dijkobjecten vullen { dijken[z] = new Dijk(); }   for(int z=0; z<binnendijken.length; z++)//array voor binnendijkobjecten vullen { binnendijken[z] = new Binnendijk(); }   for(int x=0; x<80; x++)//de waterachtergrond op het scherm zetten { for(int y=0; y<30; y++) { addObject(watersprepare[a], x, y); a++;//counter optellen van vorige water object }   for(int y=30; y<45; y++)//de dijkachtergrond op het scherm zetten { addObject(dijken[b], x, y); b++;//counter optellen van vorige dijk object }   for(int y=45; y<70; y++)//de binnendijkachtergrond op het scherm zetten { addObject(binnendijken[c], x, y); c++;//counter optellen van vorige binnendijk object } }   //addObjecten functie in game plaatsen addObject(new addObjecten(), 40, 2);   addObject(new OndermenuImg(),40, 74); OnderMenu ondermenu = new OnderMenu();//ondermenu met juiste character in game plaatsen addObject(ondermenu,66, 76); ondermenu.zetPlaatje(gameMode);   addObject(new Item1(), 15, 74); //Itemweergave + prijzen toevoegen addObject(new Item(), 15,74); if(gameMode >= 2) { addObject(new Item2(), 21, 74); addObject(new Item3(), 27, 74); addObject(new Meeuw(), 30, 10); } if(gameMode == 3) { addObject(new Meeuw(), 50, 10); } if(gameMode == 1) { addObject(new Item2Bezet(), 21, 74); addObject(new Item3Bezet(), 27, 74); } addObject(new Item4(), 33, 74); addObject(new Item5(), 39, 74); addObject(new Geld1(), 15, 78); addObject(new Geld2(), 21, 78); addObject(new Geld3(), 27, 78); addObject(new Geld4(), 33, 78); addObject(new Geld5(), 39, 78);   addObject(new Score() , 11, 74);//score telling in game plaatsen   addObject(new Auto(), 0, 60);//auto ter decoratie toevoegen aan game   addObject(new Meeuw(), 10, 10);// meeuw ter decoratie toevoegen aan game   addObject(new Muteingame(), 77, 67);//mute knop in-game   addObject(player, 40, 50);//player toevoegen aan game player.setGameMode3(gameMode);//juiste plaatje voor player goedzetten   for(int i=0; i<gameMode; i++)//aantal arrays met water aanmaken, aantal arrays is gelijk aan game mode { waters.add(i, new ArrayList<Water>()); waters.get(i).add(0, new Water()); Water water0 = waters.get(i).get(0); addObject(water0, 0, 0); waters.get(i).get(0).makeBegin(waters, i); } }   public Player getPlayer() { return player; }   public void act() {   if(!isPaused) { setScore(); Greenfoot.setSpeed(30); } pauseGame(); started(); }   public void pauseGame() { if(!readPause && (!Greenfoot.isKeyDown("escape"))) readPause = true; if((Greenfoot.isKeyDown("escape")) && readPause) { if(isPaused) isPaused = false; else isPaused = true; readPause = false; } }   public void setScore() { setScore(5);// iedere act score iets verhogen for(int i=0; i<waters.size(); i++)//nieuw water toevoegen aan game { waters.get(i).get(0).addNextWater(waters, i); } }   public void started() { music.playLoop(); }   public void stopped() { music.stop(); }   public void mute() { music.setVolume(0); }   public void unmute() { music.setVolume(100); } }```

car:

Code :

```import greenfoot.*;   public class Auto extends Game1 { public void act() { world theWorld = (world) getWorld(); if(!theWorld.isPaused) { move(1); setAutoImage(); } }   public void setAutoImage() { GreenfootImage autoimg = new GreenfootImage("Auto.png"); if(getX() >= (getWorld().getWidth()-3)) { getWorld().addObject(new Auto(), 0, 60); getWorld().removeObject(this); } setImage(autoimg); } }```
• January 13th, 2013, 03:47 PM
Norm
Re: Object Speed problem! (move();)
Where is the World and Game1 classes and the greenfoot package? The code can't be compiled and tested without all its parts.

You need to ask the authors of those classes how to use them to do what you want.
The timing of movements must be in those classes and you must not be using them correctly.
• January 13th, 2013, 04:10 PM
Optimizer
Re: Object Speed problem! (move();)
Decided to go for it myself. Coded this;

Code :

```public class Auto extends Game1 { private int delcount = 1;   public void act() { world theWorld = (world) getWorld(); if(!theWorld.isPaused) { delcount++; if(delcount == 4) { move(1); delcount = 1; } setAutoImage(); } }```

and it now works.

Thanks for your help though. I couldnt give all the files cuz it was too big.
Sorry!
• January 13th, 2013, 05:24 PM
Norm
Re: Object Speed problem! (move();)