Howdy, Stranger!

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

Help with sprites

edited May 2013 in Questions Posts: 23

Is there a way in codea to say,

if sprite1 is touching sprite2 then
do something


  • edited May 2013 Posts: 372

    I don't know about that but if you know the width and height of sprites then maybe you could check if the x,y coordinates of one sprite are close to the other spites coordinates. Or if you want you can make physics objects around them and use collide function. Here's the tutorial for it by @Ignatz But if the sprites are too complicated I'd say you go for the first method it's not that accurate but it can save some processing power.

  • BriarfoxBriarfox Mod
    edited May 2013 Posts: 1,542

    I used this in a simple side scroller. Nothing fancy but it worked. It returns true if collision occurs

    function collision(rectA, rectB) -- each rect is a table that holds x, y, width, height
        local ax2, bx2, ay2, by2 = rectA.x + rectA.width, rectB.x + rectB.width, rectA.y + rectA.height, rectB.y + rectB.height
        return ax2 > rectB.x and bx2 > rectA.x and ay2 > rectB.y and by2 > rectA.y
  • dave1707dave1707 Mod
    Posts: 9,982

    If the sprites are round you can use physics.body CIRCLE, but if they're irregular then you can use physics.body POLYGON.

    supportedOrientations(PORTRAIT) function setup()     c1=physics.body(CIRCLE,35)     c1.x=WIDTH/2     c1.y=HEIGHT-200     c1.restitution=1     c2=physics.body(CIRCLE,35)     c2.type=STATIC     c2.x=WIDTH/2     c2.y=400    end function draw()     background(40, 40, 50)     sprite("Planet Cute:Character Princess Girl",c1.x,c1.y)     sprite("Planet Cute:Gem Blue",c2.x,c2.y) end function collide(c)     if c.state==BEGAN then         print("collision, do something")     end    end
Sign In or Register to comment.