It looks like you're new here. If you want to get involved, click one of these buttons!
--[[ Drag the shapes around with your mouse or fingers This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a> (C) 2010 - 2011 Gideros Mobile ]] local function onMouseDown(self, event) if self:hitTestPoint(event.x, event.y) then self.isFocus = true self.x0 = event.x self.y0 = event.y event:stopPropagation() end end local function onMouseMove(self, event) if self.isFocus then local dx = event.x - self.x0 local dy = event.y - self.y0 self:setX(self:getX() + dx) self:setY(self:getY() + dy) self.x0 = event.x self.y0 = event.y event:stopPropagation() end end local function onMouseUp(self, event) if self.isFocus then self.isFocus = false event:stopPropagation() end end for i=1,7 do local shape = Shape.new() shape:setLineStyle(3, 0x000000) shape:setFillStyle(Shape.SOLID, 0xabc300, 0.5) shape:beginPath() shape:moveTo(0, 0) shape:lineTo(100, 0) shape:lineTo(100, 50) shape:lineTo(0, 50) shape:closePath() shape:endPath() shape:setX(math.random(0, 320 - 100)) shape:setY(math.random(0, 480 - 50)) shape.isFocus = false shape:addEventListener(Event.MOUSE_DOWN, onMouseDown, shape) shape:addEventListener(Event.MOUSE_MOVE, onMouseMove, shape) shape:addEventListener(Event.MOUSE_UP, onMouseUp, shape) stage:addChild(shape) end local info = TextField.new(nil, "drag the shapes around with your mouse or fingers") info:setPosition(23, 50) stage:addChild(info) |
Comments
I have some questions too.
1.What about our functions' parameters "self" and "event". What does this do exactly?
2.The arguments' names is special or, can we change their names for instance john , jack. like : local function onMouseMove(john, jake)
3.In C or Java , for instance, we write a function with two arguments and then we call the function by loading some values to the arguments. However , in this function , conditions are different i think. We defined the functions with two arguments (i dont know the argument's name is strict or free) . here ,is The Number of arguments of onMouseMove function is strictly two ,or do we choose it arbitrarily? What do we load to this arguments ?
Thanks in advance
Let me try to answer your questions,
1-self refer to the object that passed to the function.
Why we need self:
If we will write a function for a specific object than it will only work only for this object(In your example objects are draggable shapes). But we are making our function to work for every object.
event is also an object that is automatically created by Gideros for our usage. like on touch,onmove. Gideros generating this object and setting some values inside this object like x,y coordinate etc.. Please see this article for details: http://www.giderosmobile.com/documentation/events.htm
2-In this example self is referred to the object shape and registered in below lines so actually self becomes shape.
I hope those answers will be satisfying for you. Please fell free to ask anything. I i know i will try to help:D
Take care
I have tried < pre> </ pre> without lang parameter it is showing like code:D
is setX , getX built-in ? Please give me a very basic link.
@talis i try to read the article which you Show , but it is difficult to grasp:(
hmmm it is difficult to point you some basic link. But i can suggest you to look to class systems and object oriented programming first of all. (Not in lua, general)
If i will find some basic tutorial i will send you.
Thanks for your attention..
http://www.giderosmobile.com/documentation/reference_manual.html -shows all of Gideros' API. setX and getX are functions of the Sprite class.
As far as handling events, have you ever worked with a system that throws errors? Essentially events are just a way to communicate between different things. There is something that "throws" an event or error and something else will be "listening" for this and when it occurs, perform whatever is specified.
So in the example above, since you are listening for mouse moves, the function onMouseMove will occur every time the mouse move is "thrown". In this case we're dealing with a built-in event (mouse move), but the idea works the same even if doing custom events.
Thanks in advance
For example, @zvardin you say 'e' instead of "event" .
How can the computer understand that 'e' is an Event. It understands it because of being second argument of listener function or not??
The same logic goes with e object whihc is used in java in errors, like e.message gives you the error code. The same logic they are an object created by the system itself.
"2.The arguments' names is special or, can we change their names for instance john , jack. like : local function onMouseMove(john, jake)"
I was just trying to explain that you can rename the arguments to whatever you like. I use 'e' out of personal preference because it is somewhat standard in a lot of languages.