Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  Help: Moving image leaving trail on-screen  (Read 988 times)
0 Members and 1 Guest are viewing this topic.
Offline thelordofcheese

Junior Newbie




Java games rock!


« Posted 2005-01-12 11:29:47 »

I've just started to learn Java for mobile devices, and i've been playing around with simple MIDP1.0.3 games.

The problem is this, i have one square in the center of the screen, and on the X and Y axis i have targets.
By pressing the up, down, left, right keys  a small shot is fired in the corresponding direction.

these shots are colouered pixels of about 2 high by 2 wide.
when they are launched the game draws in their path [a straigth line along the appropriate axis] the game leaves in all the images it draws during it's movment.

So instead of having a shot fly up the screen, i have a trail leading to the top of the screen.

Deleting the object only removes the dot that remians on the end of the trail, not any of the ones that are before it.


These are the areas of code that i think have the errant instructions in them
[excuse the poor quality of the code, i'm still trying to get my head arouns Java and OO]


In GameScreen.java


//  
private Vector actorList;
    private Vector NactorList;
    private Vector SactorList;
    private Vector EactorList;
    private Vector WactorList;
    private Vector ShotList;

       
        protected void cycle()
              {

                if (lastCycleTime > 0)
        {
        msSinceLastCycle = System.currentTimeMillis() - lastCycleTime;

        // cycle all the actors
       
        // North actors
 
          // shot actors
          
          for (int i = 0; i < ShotList.size(); i++)
        {
           Actor a = (Actor) ShotList.elementAt(i);
         
Actor N = (Actor) NactorList.elementAt((NactorList.size() - 1));
           Actor S = (Actor) SactorList.elementAt((SactorList.size() - 1));
           Actor E = (Actor) EactorList.elementAt((EactorList.size() - 1));
           Actor W = (Actor) WactorList.elementAt((WactorList.size() - 1));
           
           
           System.out.println("*********[Cycle]*************");
           a.cycle((int)msSinceLastCycle);
                 System.out.println("*********[End cycle]*********");
           // check if any hit the oncomming cubes
           if ((N.isCollidingWith(a) && N != a) ||((S.isCollidingWith(a) && S != a))
           || (E.isCollidingWith(a) && E != a) ||(W.isCollidingWith(a) && W != a))
                 {
           // yar! it's a hit
             
           
           System.out.println("HIT!");

           // if so, remove the brick
           
          ShotList.removeElementAt(i);
            // remove the shot
             }
             
             
           System.out.println("ShotList.size() " + ShotList.size());
           
              }
              
              
          }
          
          
          
     }
     lastCycleTime = System.currentTimeMillis();      
                    
                    
                    
           }


In GameScreen.java

    private void renderWorld()
{   
     // this is called every cycle to paint the world
     // first to an offscreen buffer, then to the screen
     
       Graphics osg = offScreenBuffer.getGraphics();
   
       
        // there is very little to draw to begin with,
        // once we render the actors later then we'll be doing much more
       
        // so draw the score line
       
       osg.setColor(0, 0, 0);
     osg.fillRect(0, getHeight() - 10, getWidth(), getHeight());
     osg.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL));
     osg.setColor(0x00ffffff);
       
     
     osg.setColor(0x00ffffff);
     osg.drawString("CPS:  " + cps , 5, getHeight() - 10 + 1,
                    Graphics.LEFT | Graphics.TOP);
                   
                   
      for (int i=0; i < actorList.size(); i++)
     {
        Actor a = (Actor)actorList.elementAt(i);
        a.render(osg);
     }  
     
     
     // draw the 'attacking' blocks
                 
         for (int i=0; i < NactorList.size(); i++)
     {
        Actor a = (Actor)NactorList.elementAt(i);
        a.render(osg);
     }              
     
               
         for (int i=0; i < SactorList.size(); i++)
     {
        Actor a = (Actor)SactorList.elementAt(i);
        a.render(osg);
     }      
     
               
         for (int i=0; i < EactorList.size(); i++)
     {
        Actor a = (Actor)EactorList.elementAt(i);
        a.render(osg);
     }      
     
               
         for (int i=0; i < WactorList.size(); i++)
     {
        Actor a = (Actor)WactorList.elementAt(i);
       
        a.render(osg);
     }      
                   
     
     // render the shots
     
     
      for (int i=0; i < ShotList.size(); i++)
     {
       
           System.out.println("**************[Start Shots!]******************");
           Actor a = (Actor)ShotList.elementAt(i);
           a.render(osg);
         System.out.println("**************[End Shots!]******************");
    }
  }  




And finally in ShotActor.java


public void cycle(long deltaMS)
  {
     long ticks = (deltaMS + fluff) / 100;

     // remember the bit we missed
     fluff += (deltaMS - (ticks * 100));

     // move based on our speed in pixels per ticks
     if (ticks > 0)
     {
           
           int move = (int)(10 * ticks);
     switch(Direction)
           {
                   case 0:
                   System.out.println("Moving Down");
                 setY(move);
                 break;
                 case 1:
                 System.out.println("Moving up");
                 setY(move);
                 break;
                 case 2:
                 System.out.println("Moving Right");
                 setX(move);
                 break;
                 case 3:
                 System.out.println("Moving left");
                 setX(move);
                 break;
           }
        }  
       
    }
     




thanks in advance for any help i recieve.


1  
Offline wooD

Senior Newbie




J2ME Developer


« Reply #1 - Posted 2005-01-13 15:18:11 »

Try clearing the offscreen bitmap before repainting it.
1  
2  
3  
Graphics osg = offScreenBuffer.getGraphics(); 
osg.setColor(0, 0, 0);
osg.fillRect(0, 0, getWidth(), getHeight());


You were already doing this for the status area, but I think you have to do it for the entire bitmap.

If you just clear the entire map, you don't need this status area clearing anymore:
1  
2  
       osg.setColor(0, 0, 0);
      osg.fillRect(0, getHeight() - 10, getWidth(), getHeight());


but this next part would be more correct and shouldn't actually change performance any:
1  
2  
       osg.setColor(0, 0, 0);
      osg.fillRect(0, getHeight() - 10, getWidth(), 10);


fillRect(int x, int y, int height, int width);
Where height and width are the width of the fill area. Just for future reference since you say you are new.

Hope that helps,
Wood

Offline thelordofcheese

Junior Newbie




Java games rock!


« Reply #2 - Posted 2005-01-14 17:37:03 »

Thanks, that solved it.

i owe you, it's been driving me insane.
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!