Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Object Speed problem! (move();)

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

        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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object Speed problem! (move();)

    Can you make a small complete program the compiles, executes and shows the problem?

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

        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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object Speed problem! (move();)

    I dont know how to do that:/ im new to programming

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:


    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:

    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);
        }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object Speed problem! (move();)

    Decided to go for it myself. Coded this;

    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!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Object Speed problem! (move();)

    Glad you figured it out.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. can someone show me how to change speed of an java engine.
    By codenoob in forum Java Theory & Questions
    Replies: 19
    Last Post: December 14th, 2011, 12:26 PM
  2. How to speed up the performance of the java application
    By Sathiyan in forum Java Theory & Questions
    Replies: 4
    Last Post: December 2nd, 2011, 02:09 AM
  3. Speed reading program j2me
    By VeliDemir in forum Java SE APIs
    Replies: 1
    Last Post: May 16th, 2011, 08:17 AM
  4. Need...more...speed!
    By nPeep in forum Java Networking
    Replies: 8
    Last Post: September 6th, 2010, 11:07 AM
  5. KB/s download speed calculating
    By Koâk in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2009, 03:05 PM