safeDelete and removeFromScene on Mac or iPhone
by Zilla · in iTorque 2D · 09/03/2009 (9:15 am) · 17 replies
The following two methods work perfectly on my PC but have no effect on my Mac and the iPhone
Anyone else noticed this?
%mySprite.safeDelete(); or mySceneGraph.removeFromScene(%mySprite);
Anyone else noticed this?
#2
09/03/2009 (2:14 pm)
Running iTGB 1.2, I've not seen this behavior on either my Mac or iTouch that I've been using for testing. SafeDelete appears to work fine for me.
#3
This is the behaviour:
(You can attach it to any object on the scene and your scenegraph must be named mySceneGraph)
You can also download it here:
gymprojects.ch/zilla/images/helpScreenButton.cs
Does it work for you or can you see something wrong?
09/03/2009 (4:29 pm)
mmmmh... strange...This is the behaviour:
(You can attach it to any object on the scene and your scenegraph must be named mySceneGraph)
if (!isObject(helpScreenButtonBehavior))
{
%template = new BehaviorTemplate(helpScreenButtonBehavior);
%template.friendlyName = "Help Screen Button";
%template.behaviorType = "GUI";
%template.description = "Loads a help screen when clicked";
%template.addBehaviorField(helpScreen, "The help screen (Static Sprite) to load on mouse up", string, "");
}
function helpScreenButtonBehavior::onBehaviorAdd(%this)
{
%this.owner.setUseMouseEvents(true);
}
function helpScreenButtonBehavior::onMouseUp(%this, %modifier, %worldPos)
{
if ($isHelpScreen)
{
%helpScreen.safeDelete();
//mySceneGraph.removeFromScene(%helpScreen);
$isHelpScreen = false;
}
else
{
%helpScreen = new t2dStaticSprite() { scenegraph = mySceneGraph; };
%helpScreen.setPosition("0 0");
%helpScreen.setSize("200 200");
%spriteName = %this.helpScreen;
%helpScreen.setImageMap(%spriteName);
%helpScreen.setLayer(1);
$isHelpScreen = true;
}
}You can also download it here:
gymprojects.ch/zilla/images/helpScreenButton.cs
Does it work for you or can you see something wrong?
#4
09/03/2009 (4:44 pm)
What Craig said :)
#5
%this.helpscreen will access what you setting in the %template.addBehaviorField(helpScreen blablabla...
(That is provided you are accessing it from a method of the helpScreenButtonBehavior behavior.)
What are you trying to achieve with $isHelpScreen?
09/03/2009 (4:54 pm)
helpscreen is a behavior field.%this.helpscreen will access what you setting in the %template.addBehaviorField(helpScreen blablabla...
(That is provided you are accessing it from a method of the helpScreenButtonBehavior behavior.)
What are you trying to achieve with $isHelpScreen?
#6
But as I said, this behaviour works fine on the PC :)
@Craig
I do not have any problem with the code that I posted above, it just does work on the PC but not on a Mac.
I will try to create an example that you can download and see if it runs o.k. on another pc or mac.
09/04/2009 (4:52 am)
Thank you all for your help!But as I said, this behaviour works fine on the PC :)
@Craig
Quote:%this.helpscreen will access what you setting...Yes, and I am exactly doing this when I create the new t2dStaticSprite().
I do not have any problem with the code that I posted above, it just does work on the PC but not on a Mac.
Quote:What are you trying to achieve with $isHelpScreen?It also works fine. It is a switch: when the user clicks on my help button, the help screen appears, when he clicks the button again, the screen disappears.
I will try to create an example that you can download and see if it runs o.k. on another pc or mac.
#7
You can download and test it here.
Unzip it and load the project.t2dProj file into (i)TGB.
When you run it from a PC, you can click once on the GG logo, then another sprite appears on the screen, one more click on the GG logo, and the second sprite vanishes.
On a Mac, the second sprite won't disappear...
09/04/2009 (9:18 am)
I created an example application that demonstrates this behaviour.You can download and test it here.
Unzip it and load the project.t2dProj file into (i)TGB.
When you run it from a PC, you can click once on the GG logo, then another sprite appears on the screen, one more click on the GG logo, and the second sprite vanishes.
On a Mac, the second sprite won't disappear...
#8
- Load up, I get a black screen with a GG logo in the middle.
- I click the logo, another "1234" image appears on top.
- I click again, the "1234" image disappears
09/04/2009 (10:57 am)
Tested it and I get this behavior, tell me if it right (this is on a mac)- Load up, I get a black screen with a GG logo in the middle.
- I click the logo, another "1234" image appears on top.
- I click again, the "1234" image disappears
#9
Ha! This is exactly how it should be!
I tested it on 3 different macs and the 1234 image never disappeared (on 2 pcs that I used, the image always vanished)...
I don't understand it, but I tried another solution on the Mac that Nate suggested. I made %helpScreen a global variable and now it works!
On my macs, the function seems to lose the contents of its local variable...?
09/04/2009 (11:21 am)
@CraigHa! This is exactly how it should be!
I tested it on 3 different macs and the 1234 image never disappeared (on 2 pcs that I used, the image always vanished)...
I don't understand it, but I tried another solution on the Mac that Nate suggested. I made %helpScreen a global variable and now it works!
On my macs, the function seems to lose the contents of its local variable...?
#10
I'd personally write it more like this: (excuse any typos, typing as I go)
09/04/2009 (11:27 am)
Well %helpscreen was going out of scope by the looks of it. Remember you were declaring %helpscreen inside the onMouseUp method, so next time round (ie, second onMouseUp) it wasn't existing.I'd personally write it more like this: (excuse any typos, typing as I go)
if (!isObject(helpScreenButtonBehavior))
{
%template = new BehaviorTemplate(helpScreenButtonBehavior);
%template.friendlyName = "Help Screen Button";
%template.behaviorType = "GUI";
%template.description = "Loads a help screen when clicked";
%template.addBehaviorField(helpScreenImg, "The help screen (Static Sprite) to load on mouse up", string, "");
}
function helpScreenButtonBehavior::onBehaviorAdd(%this)
{
%this.owner.setUseMouseEvents(true);
%this.isShowingHelpScreen = false;
%this.helpScreen = "";
}
function helpScreenButtonBehavior::onMouseUp(%this, %modifier, %worldPos)
{
if (%this.isShowingHelpScreen)
{
%this.helpScreen.safeDelete();
%this.isShowingHelpScreen = false;
}
else
{
%this.helpScreen = new t2dStaticSprite()
{
scenegraph = mySceneGraph;
position = "0 0";
size = "80 80";
imageMap = %this.helpScreenImg;
Layer = "1";
};
%this.isShowingHelpScreen = true;
}
}
#11
09/04/2009 (11:29 am)
you'll want to update the behavior in your .t2d level file too if you decide to use the above (I changed the helpScreen behavior field to helpscreenImg for readability/sense)
#12
I will use your code in my behaviour :)))
Though I still do not understand why it worked on the PCs.
09/04/2009 (11:41 am)
Cool! Thank you very much :)))I will use your code in my behaviour :)))
Though I still do not understand why it worked on the PCs.
#13
09/04/2009 (11:44 am)
Not too sure, could be to do with script/optimise flags... but that seems odd unless without those flags stuff is staying around after going out of scope (shouldn't be!)
#14
I believe compiled without optimizations turned on the script variables stick around longer so you can get away with it in simple tests/scenarios. With the script optimizations it's not as forgiving of incorrect code. This is both a blessing and a curse. You find more bugs in your code instead of the it hiding them from you, but then again if it hides them like this when you do see them their impossible to track down.
09/04/2009 (11:56 am)
@zilla using variables that are not declared or have run out of scope in scripting depends in part on compiler options and in part with luck.I believe compiled without optimizations turned on the script variables stick around longer so you can get away with it in simple tests/scenarios. With the script optimizations it's not as forgiving of incorrect code. This is both a blessing and a curse. You find more bugs in your code instead of the it hiding them from you, but then again if it hides them like this when you do see them their impossible to track down.
#15
09/04/2009 (12:06 pm)
Bret said it more elegantly than I :)
#16
Thank you for your insight view :)
It always made me a bit nervous that I didn't have to declare my local and global vars in TGB like I did in other languages.
09/04/2009 (12:17 pm)
@BretThank you for your insight view :)
It always made me a bit nervous that I didn't have to declare my local and global vars in TGB like I did in other languages.
#17
"Important SafeTy Tip: With this flag set, you must initialize your variables or you may get some bogus values"
www.garagegames.com/community/forums/viewthread/83173
or this:
www.garagegames.com/community/forums/viewthread/85973
09/04/2009 (12:31 pm)
I should have read the Cheat Sheet:"Important SafeTy Tip: With this flag set, you must initialize your variables or you may get some bogus values"
www.garagegames.com/community/forums/viewthread/83173
or this:
www.garagegames.com/community/forums/viewthread/85973
Associate Craig Fortune
I've not experienced this problem at all. Are you sure you're running your most up to date compiled (.dso) scripts on the device?