Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
What does that line means ? — Gideros Forum

What does that line means ?

jimlevjimlev Member
edited August 2014 in Code snippets
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 problem
if 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 problem
If 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..." ?
My meditation plan :
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.”
🤔

Comments

  • Thx ! (Do we gain the record for the ratio of the longest question/shortest answer ? ;))
    My meditation plan :
    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.”
    🤔
  • keszeghkeszegh Member
    Accepted Answer
    i was aiming for that, surely.
    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.
  • ar2rsawseenar2rsawseen Maintainer
    edited August 2014
    well,
    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:
    if self.myMiniDeckStand.mcMultiplier and self.myMiniDeckStand.mcMultiplier:getParent() then
    	self.myMiniDeckStand.mcMultiplier:removeFromParent()
    end
    or if you would want to assign new sprite everytime:
    if self.myMiniDeckStand.mcMultiplier then
    	self.myMiniDeckStand.mcMultiplier:removeFromParent()
    	self.myMiniDeckStand.mcMultiplier = nil
    end
    :)

    Likes: jimlev

    +1 -1 (+1 / -0 )Share on Facebook
  • I often just enjoy when it work but if, with some explanations, I can understand why it works and grow, it's even better! ;)
    My meditation plan :
    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.”
    🤔
Sign In or Register to comment.