Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

BlackBerry® World™ Development

Reply
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Using Sprite Class

 

Hi all,

how can make use of Sprite class object in my Manager class? is it possible to use Graphics class of javax.microedition.lcdui in my app that extends UiApplication and push a screen extends MainScreen?? i tried but i didnt get any results. the screen remains as white screen. so let me know is there any approach or anothe alternative to make this possible.

 

 

Thanks and Regards,

PraveenG

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
RLord321
Posts: 343
Registered: ‎02-23-2009
My Device: 8700 | 8310 | BOLD | STORM

Re: Using Sprite Class

Blanc,

 

You should not (or cannot) mix the javax.microedition.lcdui classes and the net.rim.device.api.ui classes.  This will not work.  You will have to implement your own sprite class if you want to use the MainScreen or FullScreen.  This is not that difficult to do...

 

I have a sprite class that has the bitmap name and when this sprite is loaded, it checks to see if the bitmap (a PNG file) is already loaded in my graphics libray class.  If so, my graphics library class returns the reference to the bitmap and increases a usage counter by 1.  If not, it loads the PNG from my resource and increases the usage counter by one.

 

When the sprite is destroyed, it substracst 1 from the usage indictator and if it is 0, it unload the PNG file.  This saves memory.

 

The class should have x and y coordinates as well as any other attribute you think your sprite may need.  Width, height, energy, speed?  Keep in mind that when you draw your sprites, that you start with the y value and substract the height.  This way, you can sort your sprites so that a sprite in the background is not overlapping the sprite in the foreground.

 

Another thing to do is to make a draw(Graphics g) method in your main sprite class.  All of the different sprite classes will extend from it and will override the draw(Graphics g) method.

 

Let me know if you have any questions as you see I've been doing this for awhile.  Also, I do not know you background and I don't know if I have to get more specific of not.

 

Thanks

 

 

Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Using Sprite Class

    Lord,

              Can i have a detailed explanation or steps to implement my own sprite class?? is it the same thing that looks similar as shown in the below code or is there any specific implementation.

class MySprite extends javax.microedition.lcdui.Sprite { // some code } class Myscreen extends MainScreen { MySprite mysprite; // what is the code to be placed to use it?? }

 

                   If you are having any sample implementations let me know them it will be of great use to me.

 

 

Thanks and Regards,

PraveenG

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
New Developer
dsmaltz
Posts: 14
Registered: ‎06-06-2009
My Device: Not Specified

Re: Using Sprite Class

Lord,

 

Sorry to reply to such an old post, but I to am porting from J2me to net.rim.device.api.ui. I understand what you said about Sprites and thank you. What did you do for TiledLayer, LayerManager and Layer? Did you extend the Field manager? 

 

Thanks

Please use plain text.
Developer
RLord321
Posts: 343
Registered: ‎02-23-2009
My Device: 8700 | 8310 | BOLD | STORM

Re: Using Sprite Class

I must have missed this post...

 

Blanc,

 

My Sprite class has some methods like

 

 

public class MySprite
{   
   private Bitmap m_bitmap;
   private MySprite m_player;
   private int m_nX;
   private int m_nY;
   private int m_nWidth;
   private int m_nHeight;

  
   public void setPlayerSprite(MySprite playerSprite)
   {
      m_player = playerSprite; 
   }
   public void process()
   {
      if( m_player.getX() > m_nX + m_nWidth)
         m_nX += 4;

      if( m_player.getX() + m_player.getWidth() < m_nX)
         m_xX -= 4;
   }

   public void draw(Graphics g)
   {
       m_bitmap.draw(....);
   }

}


Main Engine Loop in the run method


..

int nLen = sprites.size();
for(int i = 0; i < nLen; ++i)
{
   // Call Process on each Sprite
   // Maybe you can sort the sprite for the Zorder
   // Draw the sprites
}

...

in the paint event

..
int nLen = sprites.size();
for(int i = 0; i < nLen; ++i)
{
   // Call sprite.draw( g); for all the sprites.
}
...

 

 

This is a small snippet of how the sprite class can be and notice how I'm not extending from the J2ME sprite class.

 

 

dsmaltz,

 

I always managed my layers myself.  What I would do is create one large bitmap with the different frames and then I would reference each frame in the actual code.

 

Please use plain text.