Howdy, Stranger!

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

Code not working

edited January 2015 in Questions Posts: 4

When you touch the screen the ball is supposed to move where you touched, however this is not working.
Help appreciated

Code:


function setup() physics.gravity(vec2(0,-1000)) touch=1 b1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0)) b2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT)) b3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT)) block=physics.body(CIRCLE,40) block.x=WIDTH/2 block.y=40 blockx=block.x blocky=block.y end function draw() background(0, 175, 255, 255) if touch> WIDTH/2 then blockx=blockx+4 end sprite("Documents:slime",blockx,blocky,80,80) blockx=block.x blocky=block.y end function touched(touch) if touch.state==BEGAN then block.linearVelocity=(vec2(0,600)) end touch=touch.x end
Tagged:

Comments

  • Posts: 1,976

    Use three ~s around your code to make it format correctly, otherwise it's just a big blob of code like it is right now.

    ~~~

    Your code here

    ~~~

    However, if you want to set its position, try replacing your touched function with this:

    function touched(touch)
        block.x = touch.x
        block.y = touch.y
    end
    
  • That works, but I would rather have the ball follow where you touched then just teleporting.

  • dave1707dave1707 Mod
    Posts: 7,836

    Added 3 ~'s to the code for formatting. @Conor_R12 If you want the ball to follow where you touch, then you're going to have to learn how to use tables.

  • Posts: 1,976

    @Conor_R12 Maybe something like this?

    function touched(touch)
        block.x = block.x * 0.9 + touch.x * 0.1
        block.y = block.y * 0.9 + touch.y * 0.1
    end
    

    It might look a little confusing, but it basically mixes the two values together, 90% the ball's position, and 10% the touch's position (getting closer every time you move your finger)

  • dave1707dave1707 Mod
    Posts: 7,836

    @Conor_R12 Try this.


    displayMode(FULLSCREEN) function setup() x=WIDTH/2 y=HEIGHT/2 dx,dy=0,0 end function draw() background(0) fill(255) text("Drag your finger around the screen",WIDTH/2,HEIGHT-50) x=x+dx/100 y=y+dy/100 ellipse(x,y,20,20) end function touched(t) if t.state==MOVING then dx=t.x-x dy=t.y-y end end
  • Posts: 1,976

    Actually, I hate to say it, but maybe you shouldn't use the touched function at all, and should put this line of code in draw:

    block:applyForce(vec2(CurrentTouch.x - block.x, CurrentTouch.y - block.y))
    
  • @SkyTheCoder Why do you hate to say that?

  • Posts: 1,976

    @warspyking Because usually you should avoid CurrentTouch at all costs, but in some rare cases, like here, it would actually be simpler.

  • Posts: 136

    @SkyTheCoder why do you want to avoid current touch?

  • Posts: 835

    Very buggy (IMO) @jrohanian

  • dave1707dave1707 Mod
    Posts: 7,836

    CurrentTouch works very similar to the function touched(). The major difference is CurrentTouch doesn't give an "id" value, which means that you can't use CurrentTouch to control multiple items at the same time. Other than that, you can do pretty much the same thing with CurrentTouch as with touched(). If you only have one item that you're controlling, then both CurrentTouch and touched() will work equally well.

  • Posts: 1,976

    @jrohanian People tend to get confused about them, there are a few odd things that can occur. For example, the value is a touch in the bottom left corner before you even press the screen. Their state can be BEGAN for multiple frames. They don't go away when you lift your finger. They don't support multitouch, meaning it'll use the most recent touch (which will make the coordinates jump across the screen in 1 frame), etc. the list goes on and on.

  • dave1707dave1707 Mod
    Posts: 7,836

    @SkyTheCoder The proper use of CurrentTouch is just like using the function touched(). When a program starts, the "state" of CurrentTouch is CANCELLED. When the screen is touched, it's state is BEGAN, when moving its state is MOVING, and when you lift your finger, it's state is ENDED. As I said above, CurrentTouch is like touched(), except you can't use "id" since it doesn't have one.

  • edited January 2015 Posts: 136

    @SkyTheCoder , Yeah, i've noticed this. I just consider it useful for different things. Sometimes it can cut down a chunk of code and use a few less variables.
    BEGAN lasting multiple frames is the only thing that really cuts back on its potential, though.

Sign In or Register to comment.