Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can't draw a square — Gideros Forum

Can't draw a square

QuasarCreatorQuasarCreator Member
edited July 2012 in General questions
I am trying to make a square and I am getting this error:

level1.lua:16: bad argument #1 to 'setAsBox' (b2PolygonShape expected, got number)
stack traceback:

Here is my code:
 
local square = b2.PolygonShape.setAsBox(20, 20, 0, 0, 0)
square:setFillStyle(Shape.NONE)
square:setLineStyle(1, 0x000FF, 2)
self:addChild(square)
So why am I getting this error? I can't seem to find anything I did wrong.

-Thankyou in advance!!
I am a programming noobie, so thanks in advance for helping me out and answering my questions!

Comments

  • talistalis Guru
    edited July 2012
    Did you include box2d in your lua code.?
    require "box2d"
  • Well, you seem to be trying to draw a square but using the Box2D physics object.

    In example:
    -- Include Box2D
    require "box2d"
     
    -- Create a Box2D shape for physics use--does not display unless you 
    -- use debugdraw or atatch a sprite.
    local square_b2d = b2.PolygonShape.new()
    square_b2d:setAsBox(20, 20, 0, 0, 0)
    Or if you actually want to draw just a box on the screen you could do this.
    -- Create a new shape drawing.
    local square_shape = Shape.new()
    square_shape:setFillStyle(Shape.NONE)
    square_shape:setLineStyle(1, 0x000FF, 2)
     
    -- Define a square with side length of 100
    square_shape:beginPath()
    square_shape:lineTo(0, 100)
    square_shape:lineTo(100, 100)
    square_shape:lineTo(100, 0)
    square_shape:lineTo(0, 0)
    square_shape:endPath()
     
    -- Add the square drawing to the scene.
    stage:addChild(square_shape)
    I may be misunderstanding, but to create a physics shape you would do the first example--and attach a sprite to it so you can detect collisions and use gravity and the good stuff.

    The second example just draws a square on the screen, and returns a sprite you can scale, rotate, and such.

    Hope it helps :)

    Eli

    Likes: QuasarCreator

    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
    +1 -1 (+1 / -0 )Share on Facebook
  • @Teranth - I did include require "box2d" but I wanted just a plain box so I went with your second example. Thankyou very much!
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
  • @QuasarCreator Anytime, this forum is very helpful feel free to ask anything you get stuck on :) Glad I was able to help you out.
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
Sign In or Register to comment.