Roads!

Organize People to walk on roads when you tap the screen!

Note:
Only every 2 seconds you can place an object by tapping the screen.

My Code:

--# Main
--# Main

displayMode(FULLSCREEN)
function setup()
   
    Player1NumberElexir=0
    elexir=0
    objects = {} -- creates a table for objects
    parameter.watch("CurrentTouch.y")
    parameter.watch("CurrentTouch.x")
    parameter.watch("elexir")
    parameter.watch("Player1NumberElexir")
end
function draw()   
    background(40, 40, 50)   
    rect(125,0,50,HEIGHT)
    rect(475,0,50,HEIGHT)
    elexir=elexir+1    
    if elexir==80 
    then
    Player1NumberElexir=Player1NumberElexir+1
    elexir=0
    end
     if Player1NumberElexir==11
    then
        Player1NumberElexir=10
    end
    
    for i, o in pairs(objects) do
        o:draw() -- loop through your objects table and draw
    end
end
function touched(touch) 
   if CurrentTouch.y < HEIGHT/2 and Player1NumberElexir>=2 and touch.state==BEGAN
         then
        table.insert(objects,Object(touch.x,touch.y))
        Player1NumberElexir=Player1NumberElexir-2
        
end

end
--# object
Object = class()

function Object:init(x,y)
    self.x = x
    self.y = y
end

function Object:draw()
    self.y = self.y+1
    if self.x < WIDTH/2
    then
         if self.x<100
        then
            self.x=self.x+1
        end
        if self.x>150
        then
            self.x=self.x-1
        end
        end
        if self.x > WIDTH/2
    then
         if self.x<450
        then
            self.x=self.x+1
        end
        if self.x>500
        then
            self.x=self.x-1
        end
        end
--self.y=self.y+1
    print(self.x)

    stroke(255, 255, 255, 255)
    strokeWidth(5)
    fill(29, 114, 133, 255)
 sprite("Planet Cute:Character Boy",self.x,self.y,50)
  --  if self.x > WIDTH/2
   -- then

     --   end
end

@magicskillz Interesting idea. Is this the start of a game you’re working on. Just to help a little, the sprites weren’t always getting on the roads. Also, if the screen was rotated, things didn’t work right. I modified your code a little to remove some of the bugs. I also removed CurrentTouch in the touched() function and the parameter.watch.

EDIT: I also moved the stroke, strokeWidth, and fill up into the draw() function so the roads were drawn as soon as the program started.

displayMode(FULLSCREEN)

function setup()
    Player1NumberElexir=0
    elexir=0
    objects = {} -- creates a table for objects
    parameter.watch("elexir")
    parameter.watch("Player1NumberElexir")
end

function draw()   
    background(40, 40, 50)   
    stroke(255, 255, 255, 255)
    strokeWidth(5)
    fill(29, 114, 133, 255)
    rect(125,0,50,HEIGHT)
    rect(475,0,50,HEIGHT)
    elexir=elexir+1    
    if elexir==80 then
        Player1NumberElexir=Player1NumberElexir+1
        elexir=0
    end
    if Player1NumberElexir==11 then
        Player1NumberElexir=10
    end
    for i, o in pairs(objects) do
        o:draw() -- loop through your objects table and draw
    end
end

function touched(touch) 
    if touch.y < HEIGHT/2 and Player1NumberElexir>=2 and touch.state==BEGAN then
        table.insert(objects,Object(touch.x,touch.y))
        Player1NumberElexir=Player1NumberElexir-2
    end
end

--# object
Object = class()

function Object:init(x,y)
    self.x = x
    self.y = y
end

function Object:draw()
    self.y = self.y+1
    if self.x < 325 then
        if self.x<149 then
            self.x=self.x+1
        end
        if self.x>151 then
            self.x=self.x-1
        end
    end
    if self.x > 325 then
        if self.x<499 then
            self.x=self.x+1
        end
        if self.x>501 then
            self.x=self.x-1
        end
    end
    sprite("Planet Cute:Character Boy",self.x,self.y,50)
end

@dave1707
I’m working on a copy of Clash Royale for fun. That’s why I ignored how the sprite didn’t line up with the roads perfectly.

@magicskillz I don’t play games, so I don’t know what Clash Royals looks like. I guess I could google it, but then again not. If you run into any future problems, we’re here to help.

Ok Thanks!

Speaking of questions
Guess what? I have a question, again. There is a error I tried to fix (trust me I did) but I don’t know what I’m doing wrong.

CODE


--# Main
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
function setup()
    Player1NumberElexir=0
    elexir=0
    elexir2=0
    Player2NumberElexir=0
    
    objects = {}--Tables for objects
    object2 = {}
    
    parameter.watch("elexir")
    parameter.watch("Player1NumberElexir")
    parameter.watch("CurrentTouch.y")
end

function draw()   
    background(0, 255, 36, 255)   
  --  sprite("Project:Tower",150,900,200,200)
    strokeWidth(5)
    noStroke()
    

    
    fill(0, 255, 234, 255)
    rect(0,HEIGHT/2,WIDTH, 50)
        fill(0, 0, 0, 255)
    rect(125,HEIGHT/2,50,50)
    rect(475,HEIGHT/2,50,50)
    elexir2=elexir2+1    
    if elexir2==80 then
        Player2NumberElexir=Player2NumberElexir+1
        elexir2=0
    end

   
    
    
  elexir=elexir+1    
    if elexir==80 then
        Player1NumberElexir=Player1NumberElexir+1
        elexir=0
    end
    if Player1NumberElexir==11 then
        Player1NumberElexir=10
    end
    for i, o in pairs(objects) do
        o:draw() -- loop through your objects table and draw
    end
        for i, o in pairs(Second) do
        Second:draw() -- loop through your objects table and draw
    end
end

function touched(touch) 
    if touch.y < HEIGHT/2 and Player1NumberElexir>=2 and touch.state==BEGAN then
        table.insert(objects,Object(touch.x,touch.y))
        Player1NumberElexir=Player1NumberElexir-2
    end
        if touch.y > HEIGHT/2 and Player2NumberElexir>=2 and touch.state==BEGAN then
        table.insert(object2,Second(touch.x,touch.y))
        Player2NumberElexir=Player2NumberElexir-2
    end
end



--# object
Object = class()

function Object:init(x,y)
    self.x = x
    self.y = y
    
    self.x2=x
    self.y2=y
end

function Object:draw()
    if self.y < 830
    then
    self.y = self.y-1
        end
if self.x < 325
         then
        if self.x<149 then
            self.x=self.x+1
        end
        if self.x>151 then
            self.x=self.x-1
        end
    end
    if self.x > 325 then
        if self.x<499 then
            self.x=self.x-1
        end
        if self.x>501 then
            self.x=self.x+1
        end
        end

    stroke(255, 255, 255, 255)
    strokeWidth(5)
    fill(29, 114, 133, 255)
 sprite("Planet Cute:Character Boy",self.x,self.y,50)
  --  if self.x > WIDTH/2
   -- then

     --   end
end
--# Second
Second = class()

function Second:init()  
    self.x2=x
    self.y2=y
    
end

function Second:draw()
 if self.y2 < 830
    then
    self.y2 = self.y2+1
        end
    
if self.x2 < 325
         then
        if self.x2<149 then
            self.x2=self.x2+1
        end
        if self.x2>151 then
            self.x2=self.x2-1
        end
    end
    if self.x2 > 325 then
        if self.x2<499 then
            self.x2=self.x2+1
        end
        if self.x2>501 then
            self.x2=self.x2-1
        end
        end

    stroke(255, 255, 255, 255)
    strokeWidth(5)
    fill(29, 114, 133, 255)
 sprite("Cargo Bot:Claw Arm",self.x2,self.y2,50)
    -- Codea does not automatically call this method
end

@magicskillz You don’t say what error you’re getting. It helps to know. When I run your code, I get an error in the function Second:draw(). In your loop where you call Second:draw, you should call o:draw, not Second:draw(). You shouldn’t be trying to use Second in the loop, Second is a class, not a table. You need to set up a table where you create Second objects before you try to use them.

My error:

Second:10: attempt to compare nil with number
stack traceback:
Second:10: in method ‘draw’
Main:51: in function ‘draw’

@magicskillz you also need to change Second:init() to Second:init(x,y) .