Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multidimensional Array Movement  (Read 1585 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Member





« Posted 2013-10-29 13:16:40 »

I'm making a space invaders-esk game and i have 3 rows of 11 aliens. I've down this by creating a multi-dimensional array of aliens As this is supposed to be an invasion the aliens need to move down the screen every so often, currently i'm using this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
 // update the game
   public void update()
    {      
       // schedule the aliens to move down
      alienTimer.schedule(new TimerTask(){

         public void run()
         {
             // move the aliens row by row
            moveAliens();
         }
         
       }, 1000, 2000);
     
    }

    // move the aliens row by row
   private void moveAliens()
    {
       for(int i = (aliens.length -1); i > -1; i--)
       {
          for(int j = 10; j > -1; j--)
          {
             if(aliens[i][j] == null)
             {
                // Do nothing alien doesn't exist
            }
             else
             {
                // move the alien down
               aliens[i][j].moveDown();
             }
          }
       }
    }

// Alien Move Down method
// move the alien
  public void moveDown()
   {
      getPosition().y += 5;
   }


but when i run the game the after the second delay all the aliens just move down the screen without stopping for the 2 second period. I've also tried scheduleAtFixedRate, but it had the same result.

Anyone have any ideas, and whilst your here, how could i go about having different rows move in opposite directions left and right, eg row 1 would move left, hit the screen bounce then move right, row 2 would move right, it the screen bounce, then move left.

Thanks for any help
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-10-29 14:48:05 »

With every call to update() (which happens at 60Hz or so) you schedule a new task.

So after 60 updates, you have scheduled 60 tasks that call moveAliens() after 2000ms.

After the initial 2000ms, every update/frame will effectively be calling moveAliens(), because the enormous amount of scheduled tasks.

What you need to do is call scheduleAtFixedRate and call that method only once, not for every update.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline _Scyth_

Junior Member





« Reply #2 - Posted 2013-10-30 21:01:48 »

That works wonders, now however, if the level changes i want the period variable to decrease so that there is less time between each movement, is there anyway to do that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2013-10-30 21:07:25 »

Cancel the TimerTask and reschedule a new TimerTask.

Keep in mind however that
java.util.Timer
uses a new Thread behind the scenes, and you will run into all kinds of trouble due to concurrency issues. It's better (or... mandatory for non-experts) to handle all logic on the same thread.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline _Scyth_

Junior Member





« Reply #4 - Posted 2013-10-30 21:12:42 »

If there is only one timer will it cause an issue creating multiple timertasks?
oh and in the timertask's run method, i will want to change the speed every 5 levels, so could i use
1  
2  
3  
4  
if( level.getLevel() % 5 )
{
 this.cancel();
}


then have a variable that is changed in the period parameter
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2013-10-30 21:37:43 »

The rendering and all other logic is happening on another thread, so yes, it will cause the weirdest bugs and crashes, if you let multiple threads manipulate the same objects.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline _Scyth_

Junior Member





« Reply #6 - Posted 2013-10-30 21:40:17 »

Is there a better way for me to handle the movement of the rows of aliens down the screen in a way so that i change the period between each movement?
Offline SHC
« Reply #7 - Posted 2013-10-31 14:49:48 »

I use my own timer class for these purposes. The idea is to move the aliens after counting the time.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class GTimer
{
    public int elapsedTime;

    public void update(long elapsedTime)
    {
        this.elapsedTime = elapsedTime;
    }

    public void reset()
    {
        elapsedTime = 0;
    }
}

And use the timer like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
timer.update(elapsedTime);

if (timer.elapsedTime >= frequencyToMove)
{
    moveAliensDown();
    timer.reset();
    // Increase frequency in bits
   frequencyToMove-=100;
}

Hope this helps.

Offline _Scyth_

Junior Member





« Reply #8 - Posted 2013-11-01 16:01:08 »

It does Smiley Thanks a lot Smiley
Offline lcass
« Reply #9 - Posted 2013-11-02 09:03:34 »

Question , how have you implemented that timer? the second part of the code could be placed in the main gameloop and call a function such as sectick();.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Scyth_

Junior Member





« Reply #10 - Posted 2013-11-02 16:53:12 »

i havent put it in yet, im still trying to work out how to pass the elapsed time variable, im not sure whether system.currentTimeMilliseconds() will work or not
Offline SHC
« Reply #11 - Posted 2013-11-02 17:10:18 »

I use this method.

1  
2  
3  
4  
public long getCurrentTime()
{
    return (long)(System.nanoTime()/1000000);
}

Hope this helps.

Offline _Scyth_

Junior Member





« Reply #12 - Posted 2013-11-03 17:19:22 »

SHC do you just pass getCurrentTime() to the update() eg. update(getCurrentTime);?
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #13 - Posted 2013-11-03 17:24:00 »

No, you should be passing the delta into the update method. The currentTime method literally gives you the current time. You should always use the delta when updating your graphics because its time it takes to change frames. You should be passing elapsedTime I believe.

Offline SHC
« Reply #14 - Posted 2013-11-04 14:39:24 »

SHC do you just pass getCurrentTime() to the update() eg. update(getCurrentTime);?

No, it's just a method to calculate the time. Just use the time difference between the frames or simply called, the delta time. I just posted it since you asked about the System.currentTimeMillis() method.

Offline _Scyth_

Junior Member





« Reply #15 - Posted 2013-11-04 17:13:11 »

My gameloop dosnt have an elapsed time, how will i do about getting that :/
Offline SHC
« Reply #16 - Posted 2013-11-05 13:45:02 »

My gameloop dosnt have an elapsed time, how will i do about getting that :/

Fixed time-step game loop? Then you can use
UPDATES_PER_SECOND
as the elapsed time.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2013-11-05 17:32:54 »

You mean 1.0 / UPDATES_PER_SECOND

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline _Scyth_

Junior Member





« Reply #18 - Posted 2013-11-05 23:22:03 »

Err... I'm not too sure :S
This is my game loop code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
      
   // constants for the game loop, fps etc
  final int TICKS_PER_SECOND = 30;
   final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;

   // longs to control the interpolation
  long next_game_tick = System.currentTimeMillis();
   
   long sleep_time = 0;

   // Boolean to say game is running
  boolean game_is_running = true;


   // game loop class
   private class Logic extends Thread
    {
       // constructor for game loop
      public Logic()
       {
          // start the thread
          start();
         }
       // what to do when the thread starts
        public void run()
         {
            // the loop itself
           while( game_is_running )
           {
               // run the update method
             update();
              //  repaint the screen
             invalidate();
           
              // interpolation code
             next_game_tick += SKIP_TICKS;
              sleep_time = next_game_tick - System.currentTimeMillis();
             
              if( sleep_time >= 0 )
              {
                 try
                 {   // stop the thread for a short period of time so that it isn't always running
                   Thread.sleep(sleep_time);
                 }
                 catch(InterruptedException e)
                 {
                    e.printStackTrace();
                 }
              }
              else
              {
                 // Running behind
             }
           }
         }
    }
Offline SHC
« Reply #19 - Posted 2013-11-06 03:07:39 »

Yes. You can pass
SKIP_TICKS
to the update method and use it as elapsed time.

Offline _Scyth_

Junior Member





« Reply #20 - Posted 2013-11-06 17:10:16 »

Thanks for the help Smiley
Really Appreciated Smiley
Offline _Scyth_

Junior Member





« Reply #21 - Posted 2013-11-06 19:29:37 »

Using SKIP_TICKS doesnt work :s, if i pass next_game_tick it works but they move down the screen too fast.
Offline SHC
« Reply #22 - Posted 2013-11-07 02:17:01 »

Maybe something is wrong. Here's my game loop.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
/**
 * Implements the game loop. User mustn't call this method.
 */

public final void run()
{
    // Game loop initialization
   long now = getCurrentTime();
    long step_size = 1000 / Global.STEPS_FOR_SECOND;
    long game_time = getCurrentTime();

    // FPS counter
   int frames = 0;
    long lastFPSCount = getCurrentTime();

    // UPD counter
   int updates = 0;
    long lastUPDCount = getCurrentTime();

    // Frame-skips
   int loops = 0;

    while (running)
    {
        loops = 0;
        now = getCurrentTime();

        while (now > game_time && loops < 2)
        {
            updateGame(1000 / Global.ACTUAL_STEPS_FOR_SECOND);
            game_time += step_size;

            // calculate update count
           updates++;
            loops++;

            if (now - lastUPDCount > 1000)
            {
                lastUPDCount = now;
                Global.ACTUAL_STEPS_FOR_SECOND = updates;
                updates = 0;
            }
        }
        displayGame();

        // FPS counter
       frames++;

        if (now - lastFPSCount > 1000)
        {
            lastFPSCount = now;
            Global.FRAMES_PER_SECOND = frames;
            frames = 0;
        }
    }
}

It's similar to yours but it works for me.

Offline _Scyth_

Junior Member





« Reply #23 - Posted 2013-11-07 22:30:56 »

Nope, the aliens just move until they hit they bottom.

All i want is for them to move periodically, why does something so simple in concept turn out to be complicated? :/

Concept would be:
1. Move Down
2. Wait Duration Of Delay
3. Move Down

loop until game ends :S
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!