using external library

I have code for four circle object to move left and right constantly this way:

[code] local function batter()

local circle1 = display.newCircle(150,130,7)
circle1.strokeWidth = 1
circle1:setFillColor(0,225,0)
circle1:setStrokeColor(0,0,0)
physics.addBody(circle1, { density = 0, friction = 0, bounce = 0, })

local circle2 = display.newCircle(330,150,7)
circle2.strokeWidth = 1
circle2:setFillColor(0,225,0)
circle2:setStrokeColor(0,0,0)

physics.addBody(circle2, { density = 0, friction = 0, bounce = 0, })

local circle3 = display.newCircle(150,170,7)
circle3.strokeWidth = 1
circle3:setFillColor(0,225,0)
circle3:setStrokeColor(0,0,0)

physics.addBody(circle3, { density = 0, friction = 0, bounce = 0, })

local circle4 = display.newCircle(330,190,7)
circle4.strokeWidth = 1
circle4:setFillColor(0,225,0)
circle4:setStrokeColor(0,0,0)

physics.addBody(circle4, { density = 0, friction = 0, bounce = 0, })

local move_timer = nil
local trans = nil

local function move_one()
print(“move_one”)

local function move_two()
print(“move_two”)

trans = transition.to(circle1, {time=1200, x=150, y=130, onComplete=move_one})
end

trans = transition.to(circle1, {time=1200, x=330, y=130, onComplete=move_two})
end
move_one()
end

batter() ]]

local function batter1()
local circle2 = display.newCircle(330,150,7)
circle2.strokeWidth = 1
circle2:setFillColor(0,225,0)
circle2:setStrokeColor(0,0,0)
physics.addBody(circle2, { density = 0, friction = 0, bounce = 0, })

local move_timer1 = nil
local trans1 = nil

local function move_one1()
print(“move_one”)

local function move_two1()
print(“move_two”)

trans1 = transition.to(circle2, {time=1200, x=330, y=150, onComplete=move_one1})
end

trans1 = transition.to(circle2, {time=1200, x=150, y=150, onComplete=move_two1})
end
move_one1()
end

batter1()

local function batter2()
local circle3 = display.newCircle(150,170,7)
circle3.strokeWidth = 1
circle3:setFillColor(0,225,0)
circle3:setStrokeColor(0,0,0)

physics.addBody(circle3, { density = 0, friction = 0, bounce = 0, })

local move_timer2 = nil
local trans2 = nil

local function move_one2()
print(“move_one”)

local function move_two2()
print(“move_two”)

trans2 = transition.to(circle3, {time=1200, x=150, y=170, onComplete=move_one2})
end

trans2 = transition.to(circle3, {time=1200, x=330, y=170, onComplete=move_two2})
end
move_one2()
end

batter2()

local function batter3()
local circle4 = display.newCircle(330,190,7)
circle4.strokeWidth = 1
circle4:setFillColor(0,225,0)
circle4:setStrokeColor(0,0,0)

physics.addBody(circle4, { density = 0, friction = 0, bounce = 0, })

local move_timer3 = nil
local trans3 = nil

local function move_one3()
print(“move_one”)

local function move_two3()
print(“move_two”)

trans3 = transition.to(circle4, {time=1200, x=330, y=190, onComplete=move_one3})
end

trans3 = transition.to(circle4, {time=1200, x=150, y=190, onComplete=move_two3})
end
move_one3()
end

batter3()
[/code]

I wanted frist and third circle to move at the same speed and pattern.
and same for second and fourth.
At first it seemed like it works. But few minutes later, the circles odd move started to be noticeable.

I thought it’s because either the codes are unclear or too long.

so I deiced to make 4 different lua files for each circles, and call it at the same time.

Here’s circle1.lua file:

module(..., package.seeall)  
  
local function batter()  
  
 local circle1 = display.newCircle(150,130,7)  
 circle1.strokeWidth = 1  
 circle1:setFillColor(0,225,0)  
 circle1:setStrokeColor(0,0,0)  
 physics.addBody(circle1, { density = 0, friction = 0, bounce = 0, })  
  
 local move\_timer = nil  
 local trans = nil  
  
 local function move\_one()  
 print("move\_one")  
  
 local function move\_two()  
 print("move\_two")  
  
 trans = transition.to(circle1, {time=1200, x=150, y=130, onComplete=move\_one})  
 end  
  
 trans = transition.to(circle1, {time=1200, x=330, y=130, onComplete=move\_two})  
 end  
 move\_one()  
end  
   
batter()  

and I called it in a main.lua file like this:

  
local external = require("Circle1")  
Circle1.batter()  
  

but it’s not working.

Please help me! [import]uid: 131469 topic_id: 23183 reply_id: 323183[/import]

Try taking “local” out of line 3 and removing line 28.

Let me know if that makes a difference :slight_smile: [import]uid: 52491 topic_id: 23183 reply_id: 92701[/import]

local external = require("Circle1")  
Circle1.batter()  
  1. In your function batter() you are creating the object circle1
  2. You are init’ing your module into external
  3. There is no reference to Circle1 whatsoever anywhere

so if you write your module correctly, you will be able to use

local external = require("Circle1")  
external.batter()  

[import]uid: 3826 topic_id: 23183 reply_id: 92719[/import]

Well spotted :slight_smile: [import]uid: 52491 topic_id: 23183 reply_id: 92729[/import]

Thank you peach pellen and jayantV.
I had to change both to fix the problem.
Thanks! [import]uid: 131469 topic_id: 23183 reply_id: 92792[/import]

now I can use the external library but using library didn’t fix the problem.

I want the circles to move like this:
Image and video hosting by TinyPic

Image and video hosting by TinyPic

but few minutes later, it becomes like this:
Image and video hosting by TinyPic

1st and 3rd, and 2nd and 4th are not moving together.
I don’t know what the problem is.
Any suggestion?
[import]uid: 131469 topic_id: 23183 reply_id: 92889[/import]

Can you add this line;

[lua]physics.setDrawMode ( “hybrid” )[/lua]

And just make sure the circles aren’t hitting each other at all? The bodies could perhaps be colliding at some point and pushing each other slightly. [import]uid: 52491 topic_id: 23183 reply_id: 92941[/import]

I think the issue is there’s a fraction of a second between each transition beginning. It doesn’t matter at first, but over time you will slowly notice the difference.
Perhaps you need some distance joint to connect the circles together which will make sure they stay stuck together.
Or at least try

batter()
batter1()
batter2()
batter3()

Together and hope they fire closely enough.
You could also only fire off 1 and 2 and then set:
circle3.x, circle3.y = circle1.x, circle1.y + 40
That’d make sure they are always together.
Add those to an EnterFrame event or something… [import]uid: 10389 topic_id: 23183 reply_id: 93185[/import]

Can you give me a detailed example or codes?

I have coded like this so far:

local objects = cir1.batter()  
cir3.batter2()  
cir2.batter1()  
cir4.batter3()  
  
circle3.x , circle3.y = circle1.x, circle1.y + 40  
  

however I’m getting this error msg:


Director ERROR: Failed to execute new( params ) function on ‘game’.

…iW11YAH95oxk++++TI/-Tmp-/TemporaryItems/124/game.lua:123: attempt to index global ‘circle1’ (a nil value)

Line 6 is line 124 in the actual coding.
Help me solve this please.
Thanks. [import]uid: 131469 topic_id: 23183 reply_id: 93994[/import]