Jump to content
  • Advertisement
Sign in to follow this  
Codejoy

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

This topic is 4723 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
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Aryndon
      Project Redemption is an semi-fantasy RPG with a linear story and an elaborate combat system.
      We are building in Unity and are currently looking animators and artists.
      What we are looking for
      -Someone who is okay with split revenue/profits when finished
      -Collaborate with others in the team. Do you have an idea/thought on what should be included? Tell us!
      -Someone who wants to work with people that are passionate about this project
      If you are interested. Please message me and I will get back to you as soon as possible! Or add me on Discord AJ#6664
    • By Aggrojag
      Hello!
      I'm working on a game that is a narrative driven dark comedy with some small aspects of platforming and puzzle solving. The project is rather small as well. It touches on topics such as suicide, mental illness, family, corruption, free-will, and redemption.
      This project is exercise in polish, this means some experimentation will be needed along with some reworking of assets as they're provided.
      This will be a revshare model.
      First, I'm looking for a 2D sprite artist, not pixelated, that can compliment the style of the attached images, and be similar to the temporary character.
      We are looking to bring on a SFX designer at this time. Full list of required SFX will be available upon request, as well as a build with all elements related to sound implemented in some form (many SFXs pulled from the web for now). Will likely require some field recording, and some pretty strange SFX for when things get weird. I imagine a good portion of these will be quite fun to create.
      Lastly, I'm looking for a male voice actor, English should be your primary language. There will be at minimum two characters that will need to be brought to life through vocals. The first voice is similar to Marvin from Hitchhiker's Guide to the Galaxy. A reference for the second voice would be a mix of Ren (Ren & Stimpy), and Android 21 (DragonBallFighterZ). Due to formatting, I'm not including YouTube links in the post, sorry.
       
      WIP Scene with our custom shaders attached (platforms are lazily placed, as this was an asset test):

      A scene with dynamic lighting and temp character:

       
      Unshaded asset:

      If you made it to the bottom, thank you, and I look forward to hearing from you.
    • By SickTwistGames
      Ok, firstly, Hi.
       
      This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.
       
      My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
      So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
      It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.
       
      Thanks
    • By ethancodes
      I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
    • By Pixeye
      I wrote an extension for unity inspector that allows to group/fold variables. 
      Available on github  , cheers!

       
  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!