×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    How to kill a sprite?

    Hi,

    What is the best way to kill a sprite?

    In my game I have created a sprite array which stores a variable number of 'food' sprites which the games main character has to collect.

    When a collision is detected between the player sprite and one of the food sprites I increase the score. But I'm just wondering what the best method is for 'killing' the sprite?

    is it something like:

    foodsprite[i] = null?

    atm, I just set the x,y co-ords to 0,0 and set visible to false. Although this gives the impression the hero has eaten the food, the food sprite still exists.

    Thanks in advance for any help,

    Phil

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to kill a sprite?

    Yes, by setting the variable to null, you over-write the reference to the object. Once there are no references to an object, it becomes garbage, and the garbage collector will ensure that the memory it occupies is released.

    Make sure you add code to check pointers for null, to avoid NullPointerExceptions.

    Graham.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Re: How to kill a sprite?

    Thanks Graham.

    How would I add code to check pointers for null?

    Is it something like:

    Code:
    if (foodsprite[i] == null)
    {
    // Do something here?
    }
    I don't think I have any code to do this at present.

    Thanks,

    Phil

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to kill a sprite?

    Well, more like:

    Code:
    if (foodsprite[i] != null) {
        // use foodsprite[i] here
    }
    Otherwise, if you try to use it, you'll get a NullPointerException.

    Graham.

Similar Threads

  1. Sprite Class
    By phil_mw60 in forum Mobile Java General
    Replies: 2
    Last Post: 2010-04-11, 22:38
  2. Sprite Issue
    By phil_mw60 in forum Mobile Java General
    Replies: 10
    Last Post: 2010-03-18, 08:57
  3. Is MIDP2.0 over Symbian OS slow?
    By epolitakis in forum Mobile Java Games
    Replies: 7
    Last Post: 2007-03-16, 09:32
  4. My Questions are not getting answered
    By earamsey in forum Symbian
    Replies: 1
    Last Post: 2003-06-04, 17:26
  5. Severe RResourceFile Error, HELP!
    By earamsey in forum Symbian
    Replies: 0
    Last Post: 2003-06-02, 19:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×