Corona Sdk: storyboard API and object alpha (opacity) - lua

i am having troubles changing an object alpha in a storyboard corona application.
When the page is loaded, the object has a default alpha of 0 (it is invisible).
Then, by clicking on a button, his alpha is set to 1 (it becomes visible).
If i leave the page, move to another storyboard page, and then get back, the object is still visible, even though i set his alpha back to 0 again with this code both in the function that gets me back to other pages:
local function gotoHomefun()
if objectname then
objectname.alpha = 0
end
storyboard.gotoScene( "home", "crossFade", 400 )
return true
end
and in the destroyScene event:
function scene:destroyScene( event )
local group = self.view
if objectname then
objectname.alpha = 0
objectname :removeSelf()
objectname = nil
end
display.remove( group )
group = nil
end
I really don't know if im doing something wrong, or i found a bug.
Any help would be really appreciated! Thanks!

Do you call the removeScene or purgeScene from other scene??
if not then your scene won't be removed unless it is a low memory situation. Try setting executing the current code in the exitScene event which gets called whenever the view is changed to new scene.
Refer to this doc for the event specific detail -
Storyboard Lifecycle Events

Related

Lua clicking button makes it disappear

I'm a scripting newbie and I'm trying to make a button that when you click it disappears I have a local script as the child of a text button, this is the code I'm using.
local button = script.Parent
local function onButtonActivated()
print("Button activated!")
game.StarterGui.ScreenGui.TextButton.Transparency = 1
end
How do I make it so that the computer does the function when the button is clicked?
Check out the docs for TextButtons.
You simply need to connect your code to the Activated signal of the button.
button.Activated:Connect(onButtonActivated)
On a separate note, there's an issue with your function as well. You are modifying the button template that is in StarterGui, not the one that the player sees. UI elements are copied from StarterGui into each player's PlayerGui when the Player spawns.
To access the actual button you are trying to turn invisible, you can use relative paths, like how you defined the button variable, or give the full path to the button.
local button = script.Parent
local function onButtonActivated()
button.Transparency = 1
-- or
local player = game.Players.LocalPlayer
local btn = player.PlayerGui.ScreenGui.TextButton
btn.Transparency = 1
end
button.Activated:Connect(onButtonActivated)
There are a few ways to check if a button is clicked.
The main way is UIButton.Activated. It works the exact same as MouseButton1Click.
Something around the lines of:
button.Activated:Connect(onButtonActivated);
If this helped, you should accept either my answer or Kylaaa's answer.
try this
first make all the onButtonActivated
then instead of transperency use:
button.Visible = false
it works for me

Is my simple Lua script hanging on the first task?

I have the following code that I am not able to test very well due to the testing window being so short. It is for an iPhone autotouch script that will scan a coordinate for a color and tap when the color changes to black. I added a second component to the script which functions similarly, it detects a color on the screen then taps on a specified coordinate. Is the code formatted in a way where when the first color is detected and the tap occurs, the code advances to the second portion of the code?
colorone is the variable for the first page, and colortwo is the variable for the second page (the page displayed after the first colorone button is tapped).
Thank you in advance
local colorone = getColor(165, 1566)
local colortwo = getColor(652, 2301)
repeat
colortwo = getColor(652, 2301)
until( colortwo == 0 )
tap(652, 2301);
repeat
colorone = getColor(165, 1566)
until( colorone == 0 )
tap(920, 1688);

Button Keeps Vanishing

My App Basically has a start screen where the user enters how many exercise reps they want to do. I use this as my max variable, and then I have a button which activates a counter mechanism which adds up to max value. When it equals max value, the UI resets and I am meant to get back to Screen 1.
I have been working this problem for over an hour and I can't figure out why my button doesn't reappear when the clicker value = user input(max value).
I have coded it to do so in
ButtonScreen1.hidden = false
UserInputScreen1.hidden = false
LabelScreen1.hidden = false
But the Button won't reappear. But it appears at the start, so it's not a view controller issue to my knowledge.
Images (Top One Is Without Button which Should be Present at the End of the Application) (Bottom One is What It Looks like as it Starts which I am trying to replicate at the End)
For Full Code

Play movie clip, have it wait until end of main movie then restart

I have a flash banner that includes a movie clip of the logo being animated. I want the logo animation to only run at the beginning of the main "movie". Currently I have to find the length of the entire movie (for ex. 500 frames) then put a key frame in at the 500th frame of the logo movie clip. I know there has to be a correct way to do this...Do I add a frame name at the end of the main timeline and somehow in AS in the logo movie clip say "when you reach X goToAndPlay(1);"?
Well, you can't simply do a "when you do this, do that".
There are two ways that come to my mind right now. First the (more) proper one is to add an dispatchEvent( new Event(__EVENT_TYPE__)); to the frame you want to stop on and than add a listener to the movieclip like:
mc1.addEventListener(Event.__EVENT_TYPE__, stopPlayback);
function stopPlayback( e:Event ):void{
mc1.stop();
mc1.removeEventListener(Event.__EVENT_TYPE__, stopPlayback);
}
so that when the event is dispatched you will stop playback. You can use for example an Event.COMPLETE or maybe create a custom event.
The second way is to add a Event.ENTER_FRAME event listener than check if we are back to the first frame and if we have looped go back to the last one and stop, remove the event listener and done.
Also we need to store somewhere the last frame we were on so that we know to witch we need to get back.
mc1.addEventListener(Event.ENTER_FRAME, onEF);
var lastFrame:int = 0;
function onEF( e:Event ):void{
if(mc1.currentFrame == 1){
mc1.removeEventListener(Event.ENTER_FRAME, onEF);
mc1.gotoAndStop(lastFrame);
}
lastFrame = mc1.currentFrame;
}
use the second one only if you cant add a event dispatch to the movie clip (or a simple stop() call ;) )
Hope it helps
Cheers.

Error playing a frame for the third time

I have a flash file where the first frame contains two buttons, one of which takes the user to the second frame, and the other takes them to the third frame. At each of those frames, various text fields and variables can be manipulated via SimpleButtons. Both Frame 2 and Frame 3 have "back" buttons to take them back to Frame 1.
Currently when the user navigates back to Frame 1 for the second time (thus playing it for a third time), my second button appears to no longer exist, and I receive an error. Both buttons on Frame 1 were placed via the Flash IDE. Why is my button popping out of existence, when it did perfectly fine the previous two times? Below is my code for Frame 1. The "back" buttons simple remove event listeners and then call gotoAndStop(1)
var inited:Boolean;
var cache:SharedObject;
var libsans:Font = new libsansreg();
this.addEventListener(Event.ENTER_FRAME, frameEnter);
stats.addEventListener(MouseEvent.CLICK, statsclicked);
modules.addEventListener(MouseEvent.CLICK, modsclicked);
function initcache():void
{
this.cache = SharedObject.getLocal("RPG_Shooter")
}
function frameEnter(e:Event):void
{
if (!inited)
{
inited = true
initcache()
this.gotoAndStop(1)
}
}
function statsclicked(e:MouseEvent):void
{
this.removeEventListener(Event.ENTER_FRAME, frameEnter)
stats.removeEventListener(MouseEvent.CLICK, statsclicked)
modules.removeEventListener(MouseEvent.CLICK, modsclicked)
this.gotoAndStop(2)
}
function modsclicked(e:MouseEvent):void
{
this.removeEventListener(Event.ENTER_FRAME, frameEnter)
stats.removeEventListener(MouseEvent.CLICK, statsclicked)
modules.removeEventListener(MouseEvent.CLICK, modsclicked)
this.gotoAndStop(3)
}
I actually had similar problem at one point. It has to do with garbage collection which is not the best in Flash to begin with, but the IDE's compiler settings make it so much more insane. There are a couple of tricks you can try which might help.
Make sure you remove all listeners before leaving the frame.
Go to a "blank" frame (something which still has a background and styling, but no interact-able components) for even a 5/1000 of a second
Set the variable names to "null" on frame one (so if your component on frame 3 is named "foo" put foo = null on frame one)
put var foo:MovieClip on frame 1. repeat for all MovieClips which you might be using that are named ahead of time.

Resources