Hi all, Im new to gideros, been trying it out, Im doing a test where I create several sprites of the type ghost in main:
for i = 1, 10 do
ghost = ghost.new(i)
self:addChild(ghost)
end |
Now when I touch one of the sprites, it changes a global variable in ghost class to all the ghost type objects in the onMouseDown function in ghost, heres the ghost class:
ghost = gideros.class(Sprite)
striked = false
local frameList = {
getImgBase().."ghost_1.png",
getImgBase().."ghost_2.png"
}
function ghost:init(id)
print("init id: ", id)
self.id = id
strikedImg = Bitmap.new(Texture.new(getImgBase().."rip-32x32.png"))
-- create a Bitmap for each frame
self.frames = {}
for i = 1, #frameList do
self.frames[i] = Bitmap.new(Texture.new(frameList[i]))
end
self.nframes = #frameList
-- add first Bitmap as a child
self.frame = 1
self:addChild(self.frames[1])
-- subframe is used to adjust the speed of animation
self.subframe = 0
-- set initial position
self:setPosition(getAppWidth() , math.random(800) + 40)
self:setScale(2)
print("Position X : Y: ", self:getPosition())
-- set the speed of the bird
self.speedy = math.random(-500, 500) / 1000
self.speedx = math.random(2000, 4000) / 1000
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
end
function ghost:onEnterFrame()
self.subframe = self.subframe + 1
if not striked then
-- for every 10 frames, we remove the old frame and add the new frame as a child
if self.subframe > 10 then
self:removeChild(self.frames[self.frame])
self.frame = self.frame + 1
if self.frame > self.nframes then
self.frame = 1
end
self:addChild(self.frames[self.frame])
self.subframe = 0
end
end
-- get the current position
local x,y = self:getPosition()
-- change the position according to the speed
x = x - self.speedx
y = y + self.speedy
-- if the bird is out of the screen, set new speed and position
if x < -100 then
self.speedy = math.random(-500, 500) / 1000
self.speedx = math.random(2000, 4000) / 1000
x = getAppWidth()
y = math.random(800) + 40
end
-- set the new position
self:setPosition(x, y)
end
function ghost:onMouseDown(event)
if self:hitTestPoint(event.x, event.y) then
print("ID: ", self.id, " :Striked: ", striked)
if not striked then
striked = true
print("## Position: ", self:getPosition());
self.speedx = 0
self.speedy = 0
self:removeChild(self.frames[self.frame])
self:addChild(strikedImg)
end
end
end |
this test is based on the bird example, so most of the code is from there
Can someone explain to me whats going on, please?
Comments
but whats happening is when i touch the first one, the "striked" boolean gets changed to all of them, so the animation in onEnterFrame() stops for all.
I added this boolean, because without it, when I touched the first one, it changed the img, but if i touched it a second time, it would give an error, because the "self:removeChild(self.frames[self.frame])" is not a child anymore.
basically Im wondering, how do I create several instances of one object (ghost in this case) and I can change each one state (booleans, or other wise) without affecting the rest of them
only problem now, is that their getting removed when i hit the next one, they're just gone, one by one, i dont get it, since Im not removing them from the parent (scene)
I would like to keep them all there, stopped, after i hit them. So I still dont get why or how they are connected, i create a new one each time:
oh, and im sry about my english, its not my first language
And in your mouse down whrite
if self.striked == false then
self.striked = true
...
end
Instead of
if not striked then
striked = true
The best solution would be to make the Ghost a class by itself that creates and returns a new ghost, each ghost has it's own set of variables that tell you if it was stricken, it's other properties like x, y, size, (thinking more like PAC MAN type ghosts) the state, time to normalize, etc
For an idea on how to do that, look at the Birds demo that comes with Gideros, the Birds class will give you an idea of how it can be done.
Side-effect of this: the code looks small and more managable.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
@OZApps Im using the birds demo code, this ghost class is the bird class with a few changes, the result is the same, everytime I touch 1 ghost/bird, it changes its image, but it gets removed everytime i click another ghost/bird
but I got it to work, basically, the final image I want to set the ghost, I cant create it in the init :S I create the new bitmap in the onMouseDown, inside the if:
Likes: ar2rsawseen
to be a variable of the class, it needs to be created in the construtor(init) right? or can I do "ghost.striked = false" outside of init or any other function?
again, sorry about the noob questions, my background is java, trying to understand lua and gideros
tnx again
if you have a java background, then you should be fine to resolve this issue.
when you use
However what you want (in Java terms) is something like
striked = theObj
Could you try in words to explain what exactly are you trying to do? What I understand is that you will have a couple of random ghosts on the screen and then you would tap them and replace them with the RIP graphic. if that is correct then (here's pseudo code)
hope this helps you move ahead...
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
tnx again