This site requires JavaScript, please enable it in your browser!
Greenfoot back
mal
mal wrote ...

2011/12/12

Problem with Act() method in sprite class

mal mal

2011/12/12

#
Hi There, I'm having a problem with a class I developed to handle all my sprites. I've set up a *Sprite Factory*. In it are all the sprite objects and methods to iterate though the animations and return the correct frames. The code looks like this:
    public SpriteFactory()
    {
       building = new Sprite("Terrain", 6);
       specialWeapon = new Sprite("specialWeapon", 4);
       player = new Sprite("playerTest",5);
    }


    public void act(){

       updateAllSprites(); 
    }

    private void updateAllSprites(){
        
        player.updateSprite();
   
    }
    
    public GreenfootImage getPlayerImage(){
       
        return player.getImage();
    
    }
So, in the above code the sprite (called "player") is supposed to update it's image for every call of Act(); then the Player Object requests the current image from the SpriteFactory, which then returns the current image from the sprite class to the player object. This doesn't happen. The sprite class doesn't update the image at all, even though the correct method is called in the SpriteFactory's Act() method. However, if I move the method call for updating the sprite image into the getPlayerImage() method, everything updates fine. Anyone got any ideas why this is the case? Why will the Act method not update the sprite image even though the correct method call is in there? thanks, Paul.
davmac davmac

2011/12/12

#
Perhaps the act() method isn't getting called. Only actors that have been added to the world will get their act() method called automatically, so perhaps you haven't added the SpriteFactory instance into the world?
mal mal

2011/12/12

#
OK, that's most definitely the problem then. Is it good practice to add the SpriteFactory to the world even though it never appears in the game? Or should I call spriteFactory.act(); from the world class? thanks, Paul.
danpost danpost

2011/12/12

#
It is usually best to add your objects (Sprites) in the world class constructor (or called from there, usually with 'populate();'). You can still create references to those objects in the world class, and have access to those references from any actor. The methods in your SpriteFactory class can be moved to the Sprite class (with minor adjustments), and there will be no need for a SpriteFactory. Actually, as far as those methods are concerned, save the getPlayerImage() method, all can be eliminated -- just add 'updateSprite();' to the act() method in the Sprite class.
davmac davmac

2011/12/13

#
Is it good practice to add the SpriteFactory to the world even though it never appears in the game?
It probably doesn't hurt much, though as a matter of pure design principle I'd say no.
Or should I call spriteFactory.act(); from the world class?
That's certainly a possibility, though you don't need SpriteFactory to extend Actor if it is not actually an Actor, and in that case you can call the method something more appropriate than "act".
mal mal

2011/12/13

#
OK, the class doesn't extend actor, it's a completely new class, I added the Act() method myself under the assumption that it would be called automatically. I've changed it to updateSprites() and now call it from the World. Hi danpost, we decided on using a spriteFactory to represent our images so we would only have one instance of each image required for the game, then we reference that image in whatever actor requires it. Each sprite can have up to 11 images (currently) that increment every time act is called. without the spriteFactory we could be looking at loading up to 17 images per sprite. if you have 100 animated sprites per level then that's 1700 images, not including backgrounds, images, etc.... that's why we decided to use a central class to hold a single instance of each image.
You need to login to post a reply.