Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
access instance in array — Gideros Forum

access instance in array

oldschoololdschool Member
edited March 2012 in General questions
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!
+1 -1 (+1 / -4 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    well you don't have x nor y properties set for your ball objects.
    I think you've meant
    a[i]:setPosition(x, y);
  • Thanks for the reply but, either I don't understand or you don't and I can't tell which. I have a ball class which has x and y and I'm trying to access them from the main.lua. Are you telling me that I can't or there is a procedure to accomplish this I don't know? Are you saying I need to set the instances of the ball class x's and y's individually in the main.lua?

    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!
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2012
    I didn't see definition of your Ball class. I presumed you used the one in example, that doesn't have those properties.
    But yes, you should be able to access them, if they are there.
  • I think I see, I assumed that like as3 once added to the stage the instance would have x and y accessible by .x .y but your saying that I have to set them first in the class. Well thank you very much!
  • --[[ main.lua

    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!
  • ar2rsawseenar2rsawseen Maintainer
    Glad it help.
    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. :)
  • CarolineCaroline Guru
    edited March 2012
    Btw, you can format your code using the tag:
    <pre lang= quote lua quote>write code here
    Where quote lua quote is "lua".

    (Sorry about the edits, wasn't sure how to write the tag)
Sign In or Register to comment.