Hi,
I' modifying jumpballs. I want to place my ball instances in an array and control there movement in main.lua. I'm getting this error main.lua:69: bad argument #1 to 'setPosition' (number expected, got nil)
stack traceback:
Here's my code.
a = {}
-- load background image and add it to the stage
local background = Bitmap.new(Texture.new("field.png"))
stage:addChild(background)
-- create ball sprites
local ball1 = Ball.new("ball1.png")
local ball2 = Ball.new("ball2.png")
local ball3 = Ball.new("ball3.png")
local ball4 = Ball.new("ball4.png")
local ball5 = Ball.new("ball5.png")
-- and add ball sprites to the stage
stage:addChild(ball1)
stage:addChild(ball2)
stage:addChild(ball3)
stage:addChild(ball4)
stage:addChild(ball5)
a[1]=ball1;
a[2]=ball2;
a[3]=ball3;
a[4]=ball4;
a[5]=ball5;
function onEnterFrame(event)
for i=1,5 do
local x, y = a[i]:getPosition();
x = x + (a[i].xspeed * a[i].xdirection)
y = y + (a[i].yspeed * a[i].ydirection)
if x < 0 then
a[i].xdirection = 1 end
if x > 320 - a[i].width then
a[i].xdirection = -1
end
if y < 0 then
a[i].ydirection = 1
end
if y > 480 - a[i].height then
a[i].ydirection = -1
end
a[i]:setPosition(a[i].x, a[i].y);
end
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
I'm coming from AS3/flex so any help greatly appreciated!
Comments
I think you've meant
I'm used to having an entity framework for my as3 games, I want to mimic that with gideros using arrays can I access instance properties from an array? If so how?
Ultimately I want to control things from the main.lua enterframe event without using enterframes in other clases is this possible?
Again thank you and any help greatly appreciated!
But yes, you should be able to access them, if they are there.
A nice example of balls moving on the screen, using classes
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]
a = {}
b=0;
-- load background image and add it to the stage
local background = Bitmap.new(Texture.new("field.png"))
stage:addChild(background)
-- create ball sprites
local ball1 = Ball.new("ball1.png")
local ball2 = Ball.new("ball2.png")
local ball3 = Ball.new("ball3.png")
local ball4 = Ball.new("ball4.png")
local ball5 = Ball.new("ball5.png")
-- and add ball sprites to the stage
stage:addChild(ball1)
stage:addChild(ball2)
stage:addChild(ball3)
stage:addChild(ball4)
stage:addChild(ball5)
a[1]=ball1;
a[2]=ball2;
a[3]=ball3;
a[4]=ball4;
a[5]=ball5;
--added
function onEnterFrame(event)
-- i want to test if the main.lua transfers control and quits
--[[b=b+1;
if b==20 then
print(b); print("hello");
b=0;
end
]]--
for i=1,5 do
local x, y = a[i]:getPosition();
x = x + (a[i].xspeed * a[i].xdirection)
y = y + (a[i].yspeed * a[i].ydirection)
if x < 0 then
a[i].xdirection = 1 end
if x > 320 - a[i].width then
a[i].xdirection = -1
end
if y < 0 then
a[i].ydirection = 1
end
if y > 480 - a[i].height then
a[i].ydirection = -1
end
--a[i].xspeed=math.random(10);
--a[i].yspeed=4;
a[i]:setPosition(x, y);
print(i);
end
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
--[[ ball.lua
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
--]]
Ball = Core.class(Sprite)
function Ball:init(texture)
local bitmap = Bitmap.new(Texture.new(texture))
self:addChild(bitmap)
self.xdirection = 1
self.ydirection = 1
self.xspeed = math.random(40, 100) / 20
self.yspeed = math.random(40, 100) / 20
self:setX(math.random(0, 270))
self:setY(math.random(0, 430))
self.width = self:getWidth()
self.height = self:getHeight()
end
It works perfectly now thanks!
Well yeah, now that you've mentioned, nobody really uses public properties in lua classes. I guess it could be because, you can't set up private properties of class, all of them are public, so maybe it is right convention to use properties as "logically private" and wrap eveything in public methods. Receiving multiple parameters from methods also makes it easier.
(Sorry about the edits, wasn't sure how to write the tag)