Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

A sprite Disappearing

Im making a whack a bug game and i just need to make my bugs disappear after x amount of seconds. Does anyone have some code to make them disappear after x amount of seconds?

-Thanks

Tagged:

Comments

  • Posts: 186

    You can define a lifeTime variable for how long you want the bug to be alive and then test it against elapsedtime.

    function makeBug()
       bug = {}
       bug.lifeTime = 10
       bug.birthTime = elapsedTime
    end
    
    function draw()
       if elapsedTime-bug.birthTime > bug.lifeTime then
          -- remove the bug
       end
    end
    
  • Posts: 2,020

    Or, combine those two variables into one:

    lifetime = ElapsedTime +10
    ...
    if ElapsedTime>lifetime then
    
  • dave1707dave1707 Mod
    edited August 2015 Posts: 9,079

    @Conner Here's an example.

    EDIT: Added circles.

    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    function setup()
        count=100
        delay=.8    -- delay .8 seconds. Change value to desired delay time.
    end
    
    function draw()
        background(40, 40, 50)
        fill(208, 208, 208, 101)
        count=count+1
        if count>delay*60 then
            count=0
            while hold==rp do
                rp=math.random(1,25)
            end
            hold=rp
        end
        pos=0
        for x=1,5 do
            for y=1,5 do
                pos=pos+1
                ellipse(x*180,y*140,130)
                if pos==rp then
                    sprite("Planet Cute:Enemy Bug",x*180,y*140+25)
                end
            end
        end
    end
    
  • Posts: 5

    Thank you for all your advice

  • Posts: 5

    @Kirl, what do you mean by --remove bug. I need help removing the bugs

  • IgnatzIgnatz Mod
    Posts: 5,396

    you don't remove them - you just don't draw them

  • Posts: 289

    are there any ways make a sprite's apearing effects? like spinning, moving, translating

  • dave1707dave1707 Mod
    Posts: 9,079

    @firewolf You can do whatever you want with the sprite.

  • Posts: 5

    @lgnatz, ah. See im new to codea and lua and i thought you actually removed it. Thanks for telling me. How would you draw over it?

  • Posts: 81

    Codea is redrawing at up to 60 frames per second. No need to draw over it - just set the code to not draw it when necessary.

  • Posts: 5

    Thank you so much @iam3o5am

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Conner - Codea works completely differently.

    I've written a lot of posts and ebooks that may help you, here.

  • Posts: 289

    thanks Dave,could u give a effects example?

  • IgnatzIgnatz Mod
    Posts: 5,396

    Spin by rotating the sprite

    Appear and disappear by altering size or by covering the sprite with a partly transparent rectangle

    Tweens can add special effects

    If you're not sure how to do any of this, you need to learn the basics of Codea graphics. See my link above, or the wiki link at the top of this page, for help with this.

  • dave1707dave1707 Mod
    Posts: 9,079

    @firewolf Here's my above program where I spin the sprite and increase its size.

    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    function setup()
        count=100
        delay=.8    -- delay .8 seconds. Change value to desired delay time.
        rot=0
    end
    
    function draw()
        background(40, 40, 50)
        fill(208, 208, 208, 101)
        count=count+1
        if count>delay*60 then
            count=0
            while hold==rp do
                rp=math.random(1,25)
            end
            hold=rp
            rot=0
        end
        pos=0
        for x=1,5 do
            for y=1,5 do
                pos=pos+1
                ellipse(x*180,y*140,130)
                if pos==rp then
                    pushMatrix()
                    translate(x*180+20,y*140-10)
                    rot=rot+5
                    rotate(rot)
                    sprite("Planet Cute:Enemy Bug",0,0,rot/2)
                    popMatrix()
                end
            end
        end
    end
    
  • Posts: 289

    thanks Dave,its better to give more effects

  • Posts: 1,976

    @Ignatz You can also use tint() to fade in or out an image, instead of drawing a transparent rectangle over it.

Sign In or Register to comment.