Touching, Sprites and other things

You know what they say, all good things have humble beginnings or something like that, anyways code first discussion later:

-- BotSmash

-- Use this function to perform your initial setup
function setup()
    displayMode(FULLSCREEN)
    x = 0
    y = 0
    nx = 0
    ny = 0
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)
    newSprite("Planet Cute","Brown Block",x,y)
    x = x + 1
    y = y + 0.5
    fill(77, 175, 120, 255)
    textWrapWidth(500)
    font("AmericanTypewriter")
    textSize(100)
    text("swipe down to get rid of the box", 200, 700)
    
end

function newSprite(loCat,sPnM,cx,cy) -- loCat : location || sPnM : Sprite name || cx : just x || cy : just y ||
    -- spR = sprite(loCat .. ":" .. sPnM, cx, cy)
    sprite(loCat .. ":" .. sPnM, cx, cy)
    -- return spR
end

function touched(touch)
    if CurrentTouch.x <= 200 then
        nx = nx + 1
        ny = ny + 0.5
        newSprite("Planet Cute","Brown Block",nx,ny)
    end
    if touch.state == MOVING then
        x = x - 2
        y = y - 2
    end
    if touch.state == ENDED then
        x = x + 1
        y = y + 0.5
    end 
end

Well i decided, with my awe inspiring extense of physics knowledge * extreme sarcasm 8-| * that i should start with a simple game.

My first problem is that i decided to have sprites come on screen and then the user drags “down” ← (keyword)
to destroy it, but before i even got to to the “down” dragging bit i realised i couldnt make more sprites come up using my method and was wondering how the heck to keep track of sprites.

My second problem is that im not very sure how to go about getting the “general” not exact position of a sprite so the user could touch it and move it, however the solution seems to be to create a mesh or physics object and give it the texture i want to use.

My final and most important problem is that im not sure how to check which direction the user drags thier finger, the solution in my head is setting the users touch into a vector and checking the direction in which the vector travels. ← note i hardly know what a vector is

Lots of problems i know, but… a noob’s gotta learn, thanks in advance

.@Dalorbi your taking the right approach by keeping it simple.

  1. you could use a table (sometimes know as an array) to create and keep track of your sprites

  2. I’m not sure what you mean. The sprite x and y positions are the positions of the centre of the sprite on the screen and the x and y touch parameters are the position of the touch on the screen. If the current touch x position equals the current sprite x position and the current y touch position equals the current sprite y position then you have contact. You may wish to relax the boundaries on the sprite position to detect touches that are close to the centre of the sprite (for example if the x touch is greater than the x sprite minus half the width of the sprite AND the x touch is less than the x sprite plus half the width of the sprite plus the equivalent in the y direction)

  3. have a look at this discussion on swiping: http://twolivesleft.com/Codea/Talk/discussion/comment/12249#Comment_12249

Here is a bit of code which might help with 1 and 2 - it generates 10 sprites and if you touch one it moves it to the right. Not exactly what you are after but should give you a start point.


-- tablesprite

-- Use this function to perform your initial setup
function setup()
    objects={}
    numObjects=10
    for i=1,numObjects do
        size=math.random(50)+30
        objects[i]={x=math.random(WIDTH),y=math.random(WIDTH),w=size,h=size}
    end
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- Do your drawing here
    
    for i=1,#objects do
        sprite("Cargo Bot:Star Filled",objects[i].x,objects[i].y,objects[i].w,objects[i].h)
        
        if CurrentTouch.x>objects[i].x-objects[i].w/2 and CurrentTouch.x<objects[i].x+objects[i].w/2 and CurrentTouch.y>objects[i].y-objects[i].h/2 and CurrentTouch.y<objects[i].y+objects[i].h/2 then
            objects[i].x = objects[i].x + 1
        end
        
        
    end
    
end

@West thanks, and once again it seems tables are the answer to all my problems…
also +1 for the example code which i wasnt expecting also you hit the ball with the sprite position explanation. Thanks once again