Jump to content
  • Advertisement
Sign in to follow this  
Codejoy

Unity J2me midp2.0 sprite list code, first attempt.

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is some code, anyone who happens to be a j2me guru want to give me some poitners? Im coming from C++ land have to say that the lacking of pointers in j2me is both nice and sorta confusing since I got finally used to working with them. Oh well, without further ado, the PlayerOBJ class I know realize is poorly named, should be spriteListObj or something, in either case...you can see the entire sprite list class, and its usage (I hope the usage gets the point across). I do a lots of futzing with the layers, im confused as to exactly what in java is passed by reference and value, I thought it was all passed by reference, but apparently not as the layers.append(playerSprite.sprite); showed me, cause before the layer mucking was in changeTo method, I was hoping just swaping out the main sprite would work, not so after an append is done, hence all the layer mucking to remove the sprite, change its image and re append it. I am sure there is a billion other better ways to do this but ive just started to tackle this stuff like a week ago... so this is why its my first attempt. oh well without further ado:
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;


public class PlayerObj {
  private Sprite[] sprite_lists=new Sprite[16];
  public Sprite sprite;
  private int current_list; 
  private int max_list_count;
  private int vel_x;
  private int vel_y;
  
  public PlayerObj(String[] fileNames, int[] sizesX, int[] sizesY) {
      max_list_count=0;
      System.out.println("Sprite lists count: " + fileNames.length);
      for(int i=0; i<fileNames.length; i++)
      {
        try {
            sprite_lists = new Sprite(Image.createImage(fileNames), sizesX, sizesY); 
        }
        catch(IOException e) {
            System.err.println("Couldn't load image: " + fileNames);
        }
        max_list_count++;
      }
      sprite = sprite_lists[0];
  }

  public void update() {
    sprite.move(vel_x, vel_y);
  }

  public void changeTo(int newList, LayerManager theLayer) {
      theLayer.remove(sprite);      
      int tempX = sprite.getX();
      int tempY = sprite.getY();
      sprite = sprite_lists[newList];
      sprite.setPosition(tempX, tempY);      
      theLayer.insert(sprite, 0);
      
      
         /*layers.remove(playerSprite.sprite);
            playerSprite.changeTo(0);
            layers.insert(playerSprite.sprite, 0); */
  }
  
}
and its usage:
class whateverMidlet {
  private PlayerObj playerSprite;

  public void start() {
    int[] sizeX = { 22, 36};
    int[] sizeY = {32, 31};
    String[] files= { "/playerT.png", "/jump.png" };
    
    playerSprite = new PlayerObj(files, sizeX, sizeY); 
	...
    layers.append(playerSprite.sprite);


  }

  private void update() {
    // Check to see whether the game is being restarted
    if (gameOver) {
      int keyState = getKeyStates();
      if ((keyState & FIRE_PRESSED) != 0)
        // Start a new game
        newGame();

      // The game is over, so don't update anything
      return;
    }

    // Process user input to move the water layer and animate the player
    int keyState = getKeyStates();
    
    xMove = 4;
    if ((keyState & FIRE_PRESSED) != 0) {        
        if(state!=JUMP)
        {
            state=JUMP;
            playerSprite.changeTo(1, layers);
            
            yMove=-4;
        }
    }
  }
}
Any pointers or any links to code that does what I need better and more effecient would be great. This would be a great oppertunity for me to learn the profiler, but unfortuantly I don't think it would work to well trying to run all the emluator and stuff on my underpowered laptop im using for the next week or so... bah. thanks, Shane

Share this post


Link to post
Share on other sites
Advertisement
Hmm, I think there are very few j2me gurus out there. In either case, I figured I should add that I realized one flaw with the above code. I would like an update method for the objects, not on the sprites. Though the layermanager likes in its update call the sprite update. Perhaps i have to inherit a class from the layermanager, that changes up append, and a update to handle my objects instead of sprites. That might be a much better solution no?

though not sure if i can create a class that extends (inherits? my oop is rusty) from layermanager...

so still looking for some guru-esque solutions...

Share this post


Link to post
Share on other sites
You're doing all this because you want sprites with different sizes, right?

Well, this might be a bit wasteful on heap memory, but if you just make all the frames of the sprite the same size (ie, the biggest frames in the list), you could try working with the reference pixel (defineReferencePixel(x,y), setRefPixelPosition(x,y)) to keep all the frames nicely positioned. It might also need some custom collision code if you aren't using pixel level collision. It would be a tradeoff because you would need less code and classes, so unless you have a lot of these or the frame sizes are really different (it could be worth "sacrificing" a bit on the design side in this case) it might not be so bad.

shmoove

Share this post


Link to post
Share on other sites
That is part of it, but mostly im trying to mimmick the pc engine I have as close as possible.

And that allowed me to have sprite lists for my objects, so I could have a game object say "Player" that could have many different type sof animation for different states: running, shooting, jumping.
and I could just set the sprite list to what state im in. How would i mimick this with midp2.0 layermanagger, I dont see it without doing it the way I have there, wanting my one object (one player, or one particular type of enemy) to have many different types of animations..

thats what iim shooting for, the code i posted seems to work great, minus the part where I have an enemy that id like to have AI for in an update, but the problem is the layermanager intrisically (sp?) calls update on the sprites that are in it... this is great but I dont want the sprites to have the brains, i want my objects to, so I can see what finitestate an enemy is in and set the display (sprite list) appropriately, thats my ultimate goal.


Share this post


Link to post
Share on other sites
For different types of animations you could inherit from the Sprite object, and have the frame indexes for each animation, along with a method to set the right frame sequence:

public MySprite extends Sprite {
int [][] animationSequences = {{1,2,3},{4,5,6},{7,8,9}};
int [] referencePixelX = {1,2,3};
int [] referencePixelY = {1,2,3};
// you'll probably want to have some method to load these values from a data file,
// instead of hard coding them

public void changeTo(int i) {
setFrameSequence(animationSequences);
defineReferencePixel(referencePixelX,referencePixelY);
// you might also want to use defineCollisionRectangle() here
}
}


shmoove

Share this post


Link to post
Share on other sites
I will have to bone up on the animation sequences, my book didn't cover it very well. I sorta figured there was a way that one was to do it using just the sprite stuff. Perhaps im trying to mimmick my old C++ engine to closely... and doing some unnessecary abstractions.


Essentially your way all the sprite animations for any given object in the game are in one png, like for instance the player.png would hold: the running animation, attacking animation, jumping one, dying etc... correct? and then for each state i just set which sequence is which...

hmmm wow something to be said for keeping it simple. Is my assertion correct?

Btw thanks for this back and forth I can't even begin to tell you how much this helps man.

-Shane

Share this post


Link to post
Share on other sites
Quote:
Original post by Codejoy
Essentially your way all the sprite animations for any given object in the game are in one png, like for instance the player.png would hold: the running animation, attacking animation, jumping one, dying etc... correct? and then for each state i just set which sequence is which...

Exactly.

shmoove

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!