Howdy, Stranger!

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

Using tween and touch to control an object

edited October 2013 in Questions Posts: 22

So I was experimenting with using the touch and tween functions to control an object on the screen. The idea being that you would use this to control your character in a game. So far it is working fairly well. I put an object in there for you to run into and that worked ok to.

Now, I am a complete beginner and have never even attempted writing any code before I got Codea so I am sure there is a better way to do this.

Then I ran into a problem. I want our "world" to be bigger than the screen size. Like WIDTH and HEIGHT * 2. So I need the camera to follow the character (in this case a ball) around so that you can explore other areas of the world. But as soon as I tried to use translate to do it I lost touch control of the ball. Here is the code, without the translate command so you can see what I am talking about.

Any suggestions on this or am I barking up the wrong try with my control mechanism?

--# BallTouch
BallTouch = class()

function BallTouch:init()
    -- you can accept and set parameters here
    ballT = false
end

function BallTouch:touched(touch)
    -- Codea does not automatically call this method

    if touch.state == BEGAN then
        if touch.x > ball.x - 25 and touch.x < ball.x + 25 and
            touch.y > ball.y - 25 and touch.y < ball.y + 25 then
            ballT  = true        
        end
    else ballT = false    
    end
end

--# Main
-- tween test
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
    bTouch = BallTouch()
    obj = Objects()
    ball = vec2(200,200)
    movPos = false
    mov = false
    bMoving = false
end

-- This function gets called once every frame
function draw()
    background(58, 150, 233, 154)

    fill(255, 0, 0, 255)
    ellipse(ball.x,ball.y,50)   
    obj:draw()     
    if obj:isColliding(ball) then 
    bCol()
    end
end

function bStop()
    tween.stop(bMov)
    reset()
end

function bCol()
    bStop()
    canHit = false
    line = ball - obj.pos
    line = line:normalize()    
    ball = ball + line * 1
end

function reset()
    mov = false
    bMoving = false
end

function actTween(t)
   t = t 
   bMov =   tween(2,ball,{x = t.x,y = t.y},nil,reset) 

end 

function touched(touch)
    bTouch:touched(touch)    
    if ballT == true and mov == false then
        mov = true
    end
    if touch.state == MOVING and mov == true then
        movPos = true
    end
    if movPos == true and touch.state == ENDED then
        actTween(touch)        
        movPos = false  
        bMoving = true      
    end
    if ballT == true and bMoving == true then
        bStop()
    end
end    



--# Objects
Objects = class()

function Objects:init()
    -- you can accept and set parameters here
    self.pos = vec2(500,200)
    self.size = vec2(200,50)
end

function Objects:draw()
    -- Codea does not automatically call this method
    pushStyle()
    fill(254, 255, 0, 255)
    rectMode(CENTER)
    rect(self.pos.x,self.pos.y,self.size.x,self.size.y)
    popStyle()

end

function Objects:isColliding(fae)

    if fae.x > self.pos.x - self.size.x/2 and fae.x < self.pos.x + self.size.x/2 and
    fae.y > self.pos.y - self.size.y/2 and fae.y < self.pos.y + self.size.y/2 then
        return true         
    else 
        return false
    end
end

Comments

  • edited October 2013 Posts: 22

    Probably should have given the basic instructions. Touch the ball, drag finger to new location and release. You can tap the ball while it's moving to stop it and drag somewhere else to send it in a different location. It shouldn't let you go through the rectangle.
    Thanks again

  • Posts: 2,042

    @Circussmith, the problem with translate is that it doesn't translate touches, only what is being drawn

Sign In or Register to comment.