Ok, i'll do my best to explain my problem, i'd really apreciate any help!
I'm trying to create a shape at class X using a gestures module. The idea is: I have this game class and by using some values given by another class (Gestures) i wanted to create a shape at my game when the user draws a line.
Here is my code so far (this is written at the init function on the game class):
--Shape for gestures scope
local areaGestures = Shape.new()
areaGestures:setFillStyle(Shape.SOLID, 0xC0C0C0, 0.5)
areaGestures:beginPath(Shape.NON_ZERO)
areaGestures:moveTo(0, 190)
areaGestures:lineTo(application:getContentWidth(), 190)
areaGestures:lineTo(application:getContentWidth(), 220)
areaGestures:lineTo(0, 220)
areaGestures:lineTo(0, 190)
areaGestures:endPath()
self:addChild(areaGestures)
local font = TTFont.new("Fontes/tahoma.ttf", 20)
local myText = TextField.new(font, "Result: ")
myText:setPosition(50,50)
self:addChild(myText)
local function callback(name)
myText:setText(name)
end
local gest = Gestures.new({
debug = true,
draw = true,
drawColor = 0xff0000,
drawWidth = 5,
autoTrack = true,
scope = areaGestos,
allowRotation = true,
inverseShape = true,
points = 33
})
gest:addGesture("Line", {
{x = 0, y = 0},
{x = 0, y = 100}
}, callback)
gest:addGesture("ZigZag", {
{x = 0, y = 0},
{x = 50, y = 87},
{x = 100, y = 0},
{x = 150, y = 87},
}, callback)
local x = 0
local y = -100
local circle = {}
local totalPoints = 72
local step = (math.pi*2)/totalPoints
for angle = 1, totalPoints do
local newX = x*math.cos(angle*step)-y*math.sin(angle*step)
local newY = y*math.cos(angle*step)+x*math.sin(angle*step)
local point = {x = newX, y = newY}
table.insert(circle, point)
end
gest:addGesture("Circle", circle, callback)
--------------
In the Gestures class the values i need are the first and last points from the drawing made by the user that are stored in the self.points table.
The Gestures class i'm using is the one found at AppcodingEasy.com with some modifications that follows:
--Update
if match.name == "Line" then
local xIni = points[1].x;
local xFim = points[#points].x;
local yIni, yFim = 191, 220;
--local width = xFim - xIni;
--local height = yFim - yIni;
as = {}
as[i] = Shape.new()
as[i]:setFillStyle(Shape.SOLID, 00000000, 0.5)
as[i]:beginPath(Shape.NON_ZERO)
as[i]:moveTo(points[1].x, yIni)
as[i]:lineTo(points[#points].x, yIni)
as[i]:lineTo(points[#points].x, yFim)
as[i]:lineTo(points[1].x, yFim)
as[i]:lineTo(points[1].x, yIni)
as[i]:endPath()
stage:addChild(as[i])
i = i +1
floorcnt = floorcnt +1
print (floorcnt)
print (i)
if floorcnt > 1 then
stage:removeChild(as[i-1])
floorcnt = 0;
end
end
As you can see, i couldn't find a better way so i created the shape inside the Gestures class. But it just feels wrong.. The right thing would be to create the shape inside the game class, right?
So my question is, how can i access those values from another class and create shape using them? Also, would it be possible to limit the number of shapes on screen? Like, you can only have one at a time and if you draw another one the last one vanishes.
I'm sorry for the long post, i'm new to Gideros and i'm currently having a lot of problems to learn it.
Thank you for the attention!
Yuri
Comments
glad you found your way into the forum.
It will be much easier to communicate this way.
So first you have a shape, where user can draw gestures. Right?
What does the second shape do, which is created inside Gestures class?
About the code, the second shape actually is gonna be a Physical body, the idea is to create a bridge where the player's character can walk.
I'm trying to use eventDispatcher to "transfer" the points[1].x and points[#points].x from the Gestures.lua to my game class, so i can create the shape inside my game class but it isn't working...
I tried that and this happens: "attempt to index global 'gest' (a nil value)"
Why he can't find the gest?
local gest = Gestures.new({
debug = true,
draw = true,
drawColor = 0xff0000,
drawWidth = 5,
autoTrack = true,
scope = areaGestos,
allowRotation = true,
inverseShape = true,
points = 33
})
gest:addGesture("Line", {
{x = 0, y = 0},
{x = 0, y = 100}
}, callback)
And the callback function:
local function callback(name)
myText:setText(name)
--Update
local xIni = gest.points[1].x;
local xFim = gest.points[#gest.points].x;
local yIni, yFim = 191, 220;
--local width = xFim - xIni;
--local height = yFim - yIni;
as = {}
as[i] = Shape.new()
as[i]:setFillStyle(Shape.SOLID, 00000000, 0.5)
as[i]:beginPath(Shape.NON_ZERO)
as[i]:moveTo(gest.points[1].x, yIni)
as[i]:lineTo(gest.points[#gest.points].x, yIni)
as[i]:lineTo(gest.points[#gest.points].x, yFim)
as[i]:lineTo(gest.points[1].x, yFim)
as[i]:lineTo(gest.points[1].x, yIni)
as[i]:endPath()
stage:addChild(as[i])
i = i +1
floorcnt = floorcnt +1
print (floorcnt)
print (i)
if floorcnt > 1 then
stage:removeChild(as[i-1])
floorcnt = 0;
end
end
Btw, how can i post my code like you do? Mine is so ugly and unorganized.
Thanks.
http://www.giderosmobile.com/DevCenter/index.php/Forum_FAQ#How_can_I_highlight_my_Lua_code_in_the_forum_post.3F
About undefined gest, well it all depends on your code structure. The should be in the same upper scope. And gest definition should be before callback function. And probably some other circumstances I'm not aware of to make it happen.
Solution:
If you are using it inside the class, then make gest a class property:
If you are not inside any class, then try removing local before definition of gest, to make it global (I know, a dirty hack).
Here is what looks like so far:
Relevant stuff inside the game class (this is all inside the init function on the game class)
The area for drawing:
But this is happening:
"attempt to index field '?' (a nil value)"
This is reffering to this line:
try putting this before accessing points:
self.gest
self.gest.points
I don't understand, that x value should exist, why is it going like nil to the game class?
You can see it at:
About the second part of this post... limiting the number of shapes on screen.
I'm trying to only allow 1 shape per time drawn on screen. Meaning if the player draws another shape, the first one should be removed or anything like that.
For this i thought the solution would be creating an array of shapes and a counter, so i can control how much is on screen.
I declared these 2 before the init function to serve as controllers.
"bad argument #1 to 'removeChild' (Sprite expected, got nil)"
What is bugging me is that i can add the child but can't remove it... what is going on?
Thanks!
As to why your code is not working I really can't tell from the first sight, without deeper debugging
My idea was to create a physical body just the size of the bridge shape i currently have.
Still... it isn't creating the body, the fixture at least, as the character just passes through the shape and fall.
Up there, before the init function i declared a b2World to use:
local b2World
Ok, i actually got it, just switched the 'bridge:getX()' and 'bridge:getY()' for 'self.gest.points[1].x' and 'yFim' respectively. That way it gets the right position for the body!
Here's the deal: I created the shape/physical body for the bridge and both are working fine, but the game class has an parallax effect so it feels like an 2d side-scroller, moving the background always to the left.
How can i create the same effect for my bridge so it goes left with my background?
This is my callback function:
1- The shape did move to the left, but its body stayed still.
2- The shape sometimes isn't even drawn, but the body is there.
And one last thing: I tried printing the X for the bridge but somehow it returns a nil to me, even though the shape/body are created on scene, what could be causing it?
Basically solution should be to move box2d object, which you have referenced inside bridge.body
And when i try to print the bridge x up there, it still returns a nil to me.
Is it related, somehow?
Inside on enter frame do you have something like:
Using this code on the bridge on enter frame:
But the shape should have an X and Y, right?
It would be best if i just used an bitmap to create the bridge instead of the shape?
When i try to print the bridge X it just returns nil to me...
I'm still using the exact callback function that i pasted here before.
You mean inside the bridge:onEnterFrame or the game class enter frame?
I have that code inside my gameclass on enter frame, inside the bridge one i just have that portion of the code i pasted on the last post.
This is my game class onEnterFrame:
And there are two things to keep in mind, for a shape to properly position with physical body, it need to have 0,0 coordinates in center, so it should be drawn like:
And second thing, is a bridge (Which is a Shape object) a direct child of game class?
My callback function so far:
That wasn't supposed to happen, right?
So about the shape it's easy, you simple get difference between first point and last point to get height and width and divide by 2 to take half width, like:
But I tell you, it's hard to get my head around on this Maybe if you want you can share simple project where you are now, so I could experiment and make it work. Or if you don't want to share it here, you can email it to me to my username at gmail.com
I just sent you my project, hope you can give it some light. I'm sorry for all the trouble!
I managed to make the bridge body have the right size, but still only the shape moves...
Hope you can help me with that! Thanks!