Howdy, Stranger!

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

Detecting when a moving Sprite is touched

edited January 6 in Questions Posts: 9

Hi! I have this simple code which moves a Sprite back and forwards from the left to the right of the screen:

function setup()

    tweenObject =  vec3(WIDTH/4,HEIGHT/2,0)

    tween(4,tweenObject, {x = WIDTH/2+500,
                            y = HEIGHT/2,
                            z = 0},
                       {easing = tween.easing.quintInOut,
                        loop = tween.loop.pingpong})
end


function draw() 
    background(0, 0, 0, 255)

    strokeWidth(5)
    pushMatrix()


   translate(tweenObject.x,tweenObject.y)
    rotate(tweenObject.z)


    sprite("Platformer Art:Block Brick", 0,0,100,100)

    popMatrix()   

end

I want to detect when the Sprite is touched, and if it is touched before reaching the right part of the screen, to go back to the left part from wherever it was touched. I plan to have several of these sprites in different parts of the screen, so just touching the screen anywhere would not work, does anybody have an idea how to do that?

Comments

  • dave1707dave1707 Mod
    edited January 6 Posts: 7,599

    @DrSurname One way is to determine the size of a circle that would fit around the sprite. Then you can detect anytime the screen is touched within that circle with a little calculation using the x,y position of the sprite.

    PS Since your sprite is a square, you can use the size of the square instead of a circle.

  • Posts: 9

    @dave1707 But how can I calculate the x and y position of the Sprite?

  • Posts: 1,225
    @DrSurname - if you are calculating sprite x and y values in the sprite call you can't. But if you calculate the x and y positions in variables and use the variables in the sprite call you can.
  • Posts: 9

    Could you please give me an example in code? I'm not sure how to do that

  • dave1707dave1707 Mod
    Posts: 7,599

    @DrSurname You don’t have to calculate the x,y values of the sprite, the tween is doing that for you. To get the x,y values of the sprite, use tweenObject.x and tweenObject.y . You may not want to use tween for moving the sprites because you can’t easily change the direction of a tween that I know of. I’ll have to check on that to be sure. Here’s an example not using tween.

    function setup()
        x,y=0,HEIGHT/2
        xv=5
    end
    
    function draw()
        background(40, 40, 50)    
        sprite("Platformer Art:Block Brick",x,y)
        x=x+xv
        if x<0 or x>WIDTH then
            xv=-xv
        end    
    end
    
  • edited January 7 Posts: 1,225

    @drsurname - just search the forum, lots of examples - look at these posts.

  • dave1707dave1707 Mod
    Posts: 7,599

    @DrSurname Here’s something else.

    displayMode(FULLSCREEN)
    
    function setup()
        tab={}
        for z=1,9 do
            table.insert(tab,vec3(0,z*80,math.random(3,6)))
        end
    end
    
    function draw()
        background(40, 40, 50)    
        for a,b in pairs(tab) do
            sprite("Platformer Art:Block Brick",b.x,b.y)
            b.x=b.x+b.z
            if b.x<0 or b.x>WIDTH then
                b.z=-b.z
            end
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            for a,b in pairs(tab) do
                if t.x>b.x-35 and t.x<b.x+35 and t.y>b.y-35 and t.y<b.y+35 then
                    b.z=-b.z
                end
            end
        end
    end
    
  • Posts: 1,225

    @dave1707 - neat little demo.

  • dave1707dave1707 Mod
    Posts: 7,599

    @DrSurname Here’s a little game that might help you. The object is to get all the UFO’s going in the same direction by tapping on the one you want to reverse direction. If you get them going in the same direction, the number increases by 1. It starts with 2 UFO’s. The count doesn’t increase if all the UFO’s are going in the same direction because some bounced off a side. I made it to a count of 11, but couldn’t go farther.

    displayMode(FULLSCREEN)
    
    function setup()
        cnt=2
        setup1()
    end
    
    function setup1()
        delay=0
        size=HEIGHT//cnt
        tab={}
        for z=1,cnt do
            sp=0
            while sp==0 do
                sp=math.random(-6,6)
            end
            table.insert(tab,vec3(WIDTH/2,z*size-size/2,sp))
        end
    end
    
    function draw()
        background(142, 223, 212, 255)    
        for a,b in pairs(tab) do
            sprite("Space Art:UFO",b.x,b.y,size)
            b.x=b.x+b.z
            if b.x<0 or b.x>WIDTH then
                b.z=-b.z
                tch=false
            end
        end
        getDirection()
        fontSize(30)
        fill(255,0,0)
        text(#tab,WIDTH/2,HEIGHT/2)
    end
    
    function touched(t)
        if t.state==BEGAN then
            for a,b in pairs(tab) do
                if t.x>b.x-size/2 and t.x<b.x+size/2 and t.y>b.y-size/2 and t.y<b.y+size/2 then
                    b.z=-b.z
                    tch=true
                end
            end
        end
    end
    
    function getDirection()
        dir=0
        for a,b in pairs(tab) do
            if b.z>0 then
                dir=dir+1
            end
        end
        if dir==0 or dir==#tab then
            if tch then
                tch=false
                cnt=cnt+1
                setup1()
            end
        end
    end
    
Sign In or Register to comment.