Hi everyone ! Here is another question from an amateur of the empiric coding. :P
I apologize if some of my lines of code burn your eyes or trigger some vomit reflexes on the audience.
So...
Here is a function I call at the end of each turn for 'releasing' the player sprite for the next turn. I also check (via his propertie 'myMultiplier' ) if he received a 'X2" bonus this turn and, in that case, I add to him a mini .png of a "X2"
player = Core.class(Sprite)
function player:init()
self.alreadyPlayed = false
local myMiniDeckStand = Sprite.new()
self:addChild(myMiniDeckStand)
end
function player:switchMeToFree()
self.myButton:setAlpha(1)
self.alreadyPlayed = false
self.myMiniDeckStand.miniCard:removeFromParent()
if self.myMultiplier == 2 then -- the player win the X2 bonus this turn, I give him a blinking "X2" sprite
local bonusImg = Bitmap.new(Texture.new("graphics/deck/minicardMultiplier.png"),true)
local mcMultiplier = MovieClip.new{
{1, 40, bonusImg, {alpha = {0, 1}}},
{41, 79, bonusImg, {alpha = {1, 0}}}
}
mcMultiplier:setGotoAction(79, 1)
self.myMiniDeckStand:addChild(mcMultiplier)
self.myMiniDeckStand.mcMultiplier = mcMultiplier
self.myMiniDeckStand.mcMultiplier:play()
else -- the player doesn't received any bonus this turn or loosed it
print("no bonus for me")
self.myMiniDeckStand.mcMultiplier:removeFromParent()
end
end |
Everything works quite well except that... In the 'else' part.
First problemif the player already have a bonus from a previous turn, I get the "no bonus for me" but the 'removeFromParent' doesn't work (the bonus does not disappear)
Second problemIf the player never had a bonus, I logically get the error "attempt to index field 'mcMultiplier' (a nil value)"
AND THEN... in an audacious and incredibly impetuous tentative, I've added the line below and it worked !
else -- the player don't receive any bonus this turn or lose it
print("no bonus for me")
if self.myMiniDeckStand.mcMultiplier then -- WHAT DOES THIS LINE OF CODE MEANS ?!?!?:! <img class="emoji" src="https://forum.giderosmobile.com/resources/emoji/lol.png" title=":D" alt=":D" height="20" />
self.myMiniDeckStand.mcMultiplier:removeFromParent()
end
end |
So, the question : does that line means what it seems to mean (huhuu) i.e. "If that sprite exists then..." ?
Comments
Fragmenter - animated loop machine and IKONOMIKON - the memory game
SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
Totebo: “ Best quote ever.”
🤔
in fact i basically read only the last 5 lines of your post, but it seems the rest was just there so that we reach the record.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
if you would do that in loop or simply repeatedly, then even if you remove sprite from parent, it would still be saved in self.myMiniDeckStand.mcMultiplier
And when you would want to remove it second time, it would crash
So depending on what you want to achieve. If you want to reuse same mcMultiplier sprite, then you could do:
Likes: jimlev
SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
Totebo: “ Best quote ever.”
🤔