Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Hide/Unhide Object in a group
Started by damfodeky1 Mar 06 2015 06:23 PM

13 replies to this topic
hide unhide show visible
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

damfodeky1

[GLOBAL: userInfoPane.html]
damfodeky1
  • Observer

  • 12 posts
  • Corona SDK

Greetings to all members of the wonderful community.

Please, I am a newbie and have just started to utilize the power of corona.

However, I'm being confronted with this challenge, which is tearing me apart. The issue is, I have an image object displayed on a composer scene, and more so inserted into the scene's group.

However, I am trying to hide this object, using the object's alpha, and set another one visible by turning its alpha to 1, as a means of toggling the two objects. Unfortunately, I have tried so many attempts, but all have failed me. My very last action was:

local toggleJoy = function()
    joy.alpha = 0
    joy2.alpha = 1
    display.getCurrentStage():remove( joy )
    joy = nil
end

Please, I will be most grateful if any member of the forum can assist me on this issue.

Thanks in advance.



[TOPIC: post.html]
#2

corona273

[GLOBAL: userInfoPane.html]
corona273
  • Contributor

  • 482 posts
  • Enterprise

Have you tried changing the .isVisible property?

 

I don't use Composer, but presumably you could object:removeSelf instead? And you don't want to set it to nil and remove it if toggling is the goal. I haven't tried this, but maybe joy.isVisible = not(joy.isVisible) or something similar could work.



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,585 posts
  • Corona SDK

1. You probably have a 'scope and visibility' issue with your 'joy' and 'joy2' variables.  Be sure you declare them near the top of your composer scene file (BEFORE) using them.

 

2. You're going overboard when trying to remove 'joy'.  Just do this:

display.remove(joy)
joy = nil

An example of bad scope and visibility 

local function makeIt()
   local myCircle = display.newCircle( 10, 10, 10 )
end

local changeIt()
   myCircle.alpha = 0 --fails, because 'myCircle' is not visible here
end

An example of proper scope and visiblity:

local myCircle -- Now visible and within scope of both following functions.

local function makeIt()
   myCircle = display.newCircle( 10, 10, 10 )
end

local changeIt()
   myCircle.alpha = 0 --fails, because 'myCircle' is not visible here
end

Edited by roaminggamer, 07 March 2015 - 02:22 PM.


[TOPIC: post.html]
#4

Hendrix000007

[GLOBAL: userInfoPane.html]
Hendrix000007
  • Contributor

  • 177 posts
  • Corona SDK

If you start like this local joy1, joy2 then later in the script you bind joy1 and joy2 to a display object like i.e. joy = display.newCircle( -- you variables goes here ) then you maybe put them in the composer display group like this group:insert(joy) then you set the opacity: joy.alpha = 0 joy2.alpha = 1 --now joy2 is the visible object It is true that the object has to be in the same scope.......uhhhh??? yu say, this means that you have to put it all in the same bag so the variables can se and understand eachother We do this -in this case, by tying them to the composer sceene group (the variables are in the same scope) Now, when you set joy2.alpha to 0 and joy1.alpha to 0 you show the other one It is true the approach that Corona273 mention (if alpha param does not work, try isVisible param that is a boolean and not 0.0 to 1.0) this can be the answer sometimes If this does not work put it on pastebin.com so we can se the whole code

[TOPIC: post.html]
#5

damfodeky1

[GLOBAL: userInfoPane.html]
damfodeky1
  • Observer

  • 12 posts
  • Corona SDK

Corona is truely always proving my best choice ever since my discovery. I am so astonished by the non delayed massive support. The best community/forum.
First of all thanks so much for the massive support. I am so grateful to you all.
Unfortunately, I'm out of home, and will have to try all the suggestions when i get back home, so that i can report back to the forum as you have also spent your time helping me.
However, i just want to use this opportunity to ask that, is it possible to retrieve or say reference the left and top (x,y) coordinates of a row in a tableView (Not the entire tableView widget, but just a row), relative to the contentWidth and contentHeight?
My unendless thanks to you all once again!!

[TOPIC: post.html]
#6

Hendrix000007

[GLOBAL: userInfoPane.html]
Hendrix000007
  • Contributor

  • 177 posts
  • Corona SDK

I´m not shure about the widget stuff but for displayObjects in general you set the anchor.x and y (from0.0 to 1.0) both axis.

The default valuse are the middle like this:

myObject.anchorX = 0.5
myObject.anchorY = 0.5

so if you want to access the values for a object that has its referencepoint at top left, this would be first to set the correct point:

myObject.anchorX = 0
myObject.anchorY = 0

now you can print out the values like this:

print( "My objects x cords is: " .. myObject.x .. " and the y value is: " .. myObject.y)

Hope this works for widgets to, cause I´m not shure

This explains both setting anchor and retrieving objects x and y coordinates.



[TOPIC: post.html]
#7

damfodeky1

[GLOBAL: userInfoPane.html]
damfodeky1
  • Observer

  • 12 posts
  • Corona SDK

First of all thanks so much for the attention so far.

Unfortunately, I have tried all the suggestions concerning the hide and unhide issue, but all of them are not working for me. I suppose I am not doing something right in this case.

More so, @Hendrix000007, may be I am not getting you right, but what I was asking for is, for instance, if you have a tableView with a number of rows, and you touch one of the rows, how would you get that rows left (x) and top (y) coordinates.

However, in other to get what I am talking about, concerning the toggle or say hide/unhide issue, this is how I am executing the action:

 

 

 

local composer = require( "composer" )
local page = composer.newScene()

local widget = require( "widget" )
local universal = require( "myUniversal" )

 

local leftSide = display.screenOriginX;
local rightSide = display.contentWidth-display.screenOriginX;
local topSide = display.screenOriginY;
local bottomSide = display.contentHeight-display.screenOriginY;
local totalWidth = display.contentWidth-(display.screenOriginX*2);
local totalHeight = display.contentHeight-(display.screenOriginY*2);
local centerX = display.contentCenterX;
local centerY = display.contentCenterY;

 

local joy
local joy2

 

local stopWelComAudio = function()
    audio.stop( playWelcomeBackgdAudio )
    playWelcomeBackgdAudio = nil
    -- Dispose the handles from memory and 'nil' out each reference
    audio.dispose( welcomeAudio )
    welcomeAudio = nil  --prevents the handle from being used again
end

local toggleJoy = function()
    --joy.isVisible = not(joy.isVisible)
    --joy2.isVisible = (joy2.isVisible)
    joy.alpha = 0
    joy2.alpha = 1
end

function page:create(event)
    local pageComponent = self.view

    local pagePlatform = display.newRect(0,0, totalWidth, totalHeight)
            pagePlatform:setFillColor( 1, 1, 1 )
            pagePlatform.x = centerX
            pagePlatform.y = centerY
            pageComponent:insert(pagePlatform)
            pagePlatform:addEventListener("touch", abortTouch)

            local joy = display.newImageRect("joy.png", 250, 400)
                    joy.x = centerX/2
                    joy.y = centerY + centerY/3 + ((centerY/3)/5)
                    pageComponent:insert(joy)
                    joy.alpha = 1
                    joy:addEventListener("touch", abortTouch)

            local joy2 = display.newImageRect("joy2.png", 250, 400)
                    joy2.x = centerX/2
                    joy2.y = centerY + centerY/3 + ((centerY/3)/5)
                    pageComponent:insert(joy2)
                    joy2.alpha = 0
                    joy2:addEventListener("touch", abortTouch)

 

        -- Load a welcome audio stream into memory as a background audio
        local welcomeAudio = audio.loadStream( "audio/00_INTRODUCTION_unknown.wav" )
        -- Play the welcome audio stream as a background audio
        local playWelcomeBackgdAudio = audio.play( welcomeAudio )

end

 

function page:show( event )
    local pageComponent = self.view
end

 

function page:hide( event )
    local pageComponent = self.view

    --
    -- Clean up native objects
    --

end

 

function page:destroy( event )
    local pageComponent = self.view
end

page:addEventListener( "create", page )
page:addEventListener( "show", page )
page:addEventListener( "hide", page )
page:addEventListener( "destroy", page )
timer.performWithDelay( 2000, stopWelComAudio )
timer.performWithDelay( 3500, toggleJoy )

 

return page

 

Thanks once again!!

 

 

 



[TOPIC: post.html]
#8

Hendrix000007

[GLOBAL: userInfoPane.html]
Hendrix000007
  • Contributor

  • 177 posts
  • Corona SDK

the eventListener you have asigned to the joy display objects listen to a function called abortTouch and not the function called toggleJoy() so there will not happen mouch conciddering this piece of code.

I must also say that the expression --joy.isVisible = not(joy.isVisible) is something I have never seen before, do you mean:

--joy.isVisible = false ? isVisible is a boolean expression while alpha is a grade of opacity

I know that these lines is commented out but just felt like clearing out this

 

If you have a display obj like you have here you make either a table or a function-listener on the object and make it listen for an event like touch or tap and then attach it to a function. If joy should fire the toggleJoy() function you should do like this:

local joy = display.newImageRect("logo1.png", 250, 250)
joy.x = display.contentCenterX
joy.y = display.contentCenterY
local function toggleJoy(e)
if e.phase == "began" then
  if  joy.alpha == 1 then
   joy.alpha = joy.alpha - 0.5
  elseif joy.alpha < 1 then
   joy.alpha = 1
  return true
  end
end
end
joy:addEventListener("touch", toggleJoy)

This shows how to use .alpha but if you like to use the bool isVisible you can do this instead:

(I use two displayObjects that works as a switch for eachother)

local joy  = display.newImageRect("logo1.png", 250, 250)
local joy2 = display.newImageRect("logo2.png", 250, 250)
joy.x = display.contentCenterX
joy.y = display.contentCenterY * 0.5
joy2.x = display.contentCenterX
joy2.y = display.contentCenterY * 1.5
joy.isVisible = true
joy2.isVisible = false
local function toggleJoy(e)
if e.phase == "began" then
  if  joy.isVisible == true then
   joy.isVisible = false
   joy2.isVisible = true
  elseif joy.isVisible == false then
   joy.isVisible = true
   joy2.isVisible = false
  return true
  end
end
end
joy:addEventListener("touch", toggleJoy)
joy2:addEventListener("touch", toggleJoy)

If you like you can download the code here:

www.hideawayspa.no/hendrix/kurs/buttons.zip

Hope this helps you out

NB!

Keep in mind that in real life I would do the if conditions more robust this is just to point out a way to do it very stripped down :)



[TOPIC: post.html]
#9

damfodeky1

[GLOBAL: userInfoPane.html]
damfodeky1
  • Observer

  • 12 posts
  • Corona SDK

Greetings to All members, First of all let me express my sincere gratitude to you all for the support. I'm highly honoured and so appreciative to you all, espercially @Hendrix000007. I have implemented the code as shown in the "isVisible" function in the last post. However, as the listerner is executed in the "touch" events, until i touch it, the action is not performed automatically in the timer.performWithDelay as i wanted it. So the issue here is how can i make it trigger on timer event, without the user touching it. Secondly, how can i reference a display object in a 'Composer' scene group?, do i have to get all the objects on stage, and then loop to get the object's key or index to reference it? Thanks so much in advance. More so, sorry for the long delay in my reply, i travelled on an assignment mission. Meanwhile, i'm sorry, but --joy.isVisible = not(joy.isVisible) was suggested by a member on this topic, so may be i did not get it right.

[TOPIC: post.html]
#10

Hendrix000007

[GLOBAL: userInfoPane.html]
Hendrix000007
  • Contributor

  • 177 posts
  • Corona SDK

@damfodeky1

Nice to hear that I could help you out :)

You can use a timer.performWithDelay in numerous ways. The timer works like this:

The timer has 3 arguments: delay (how long in milliseconds between each repetition), the function it will trigger and finally how many times it will be repeated before it stops.

Here´s an example with the base in the prior example earlier over here:

local joy = display.newImageRect("logo1.png", 250, 250)
joy.x = display.contentCenterX
joy.y = display.contentCenterY
-- The function we later will trigger with the timer:
local function toggleJoy(e)
  if  joy.alpha == 1 then
   joy.alpha = joy.alpha - 0.5
  elseif joy.alpha < 1 then
   joy.alpha = 1
  return true
  end
end
timer.performWithDelay( 200, toggleJoy, 50 )
-- the timer will now execute the function toggleJoy 50 times with a delay of 0,2 seconds between

 

I like to mention that there are some things you should know about timers that I also point out in the script in the download file under here and I just paste it here:

---------------------------------------

REMEMBER:
timers that run while shifting pages in i.e. Composer can be tricky and lead to hickups in your scripts during runtime and it can also be hard to debug too.
My advice is to always declare a variable for the timerobject like:
local tmr = timer.performWithDelay({your args goes here})
after this is done you can adress the var tmr and cancel it right before leaving the page/script like this:
timer.cancel( tmr ) and remove the variable tmr like:
tmr=nil

------------------------------------------

If you like you can download the code here:

www.hideawayspa.no/hendrix/kurs/timer.zip 



[TOPIC: post.html]
#11

damfodeky1

[GLOBAL: userInfoPane.html]
damfodeky1
  • Observer

  • 12 posts
  • Corona SDK

@Hendrix000007 Thanks so much for the excellent support. You are undoubtedly a perfect trainer.....thank you!

Your excellent guide is truly shaping me so fast.

Everything is now intact at exactly where I have gotten to.

The directives you gave to me are working perfectly and in order. Thanks!!!

I pray that I succeed in what I intend doing quickly, so that I can acknowledge you in my app for the support. I'm most grateful.

I hope you wish to know what I am working on, but don't worry, I want to take you by a surprise :D

All the same, to everyone willing to help, how can I center a text in the newText Object?, cos I have tried all the available alignments, but to no success. What I mean is, I want to do something like this:

 

CORONA SOFTWARE DEVELOPMENT KIT!

                  THE ULTIMATE

             AMONGST THEM ALL

 

Unfortunately, I have tried every possible attempt I can, but I can't get it to work.

Thanks in advance.



[TOPIC: post.html]
#12

Hendrix000007

[GLOBAL: userInfoPane.html]
Hendrix000007
  • Contributor

  • 177 posts
  • Corona SDK

@damfodeky1

Thanx for the kind words :)

The display.newText() has the feature of center text if you first set the width and height of the textbox and the set the alignment to center.

Read about it here:

http://docs.coronalabs.com/api/library/display/newText.html

This will solve the problem :)



[TOPIC: post.html]
#13

damfodeky1

[GLOBAL: userInfoPane.html]
damfodeky1
  • Observer

  • 12 posts
  • Corona SDK

:huh: Hmmmm.........I'm getting a bit disturbed, coz it appears like I am asking too many questions, but I don't have any option too.

One, unfortunately I have assigned the width and height as suggested, but to no success. Still struggling to get it to work.

 

Two, please what am I not doing right in this my orientation handler:

local function onOrientationChange(e) 
local stage = display.getCurrentStage() 
    stage.anchorX = 0 
    stage.anchorY = 0 
    if e.type then 
    local newAngle = stage.rotation - e.delta  
    transition.to(stage, {time = 500, rotation = newAngle, transition=easing.inOutQuad}) 
    end 
end 
Runtime:addEventListener("orientation", onOrientationChange)

I want to support all orientations and also have the content resize accordingly on orientation changes, but it is not working right.

I know for sure that something is wrong somewhere with my code, but just can't figure what I am not doing right out, and that is where I am a corona newbie (precisely, a toddler to the SDK).

Any help on my two current issues please :D, at least a sample will do!

Meanwhile, please @Hendrix000007, thanks too for accepting my kind gesture, I am most appreciative for the support given!!



[TOPIC: post.html]
#14

Hendrix000007

[GLOBAL: userInfoPane.html]
Hendrix000007
  • Contributor

  • 177 posts
  • Corona SDK

Hi damfodeky1

I didn´t have the time to get the width of the textbox to work on orientatin change

What you have to do when you´re dealing with device change stuff, you need to set the device to support all orientations in build.settings first:

settings =
{
        orientation =
        {
  default = "portraitRight",
   supported =
   {
   "landscapeLeft", "landscapeRight", "portraitLeft", "portraitRight"
  }
  
},
}

And in config.lua I put scale param to letterbox:

application =
{
content =
{
  fps = 60,
  width = 320,
  height = 480,
  scale = "letterbox",
},
}

 

Here´s the code in main file:

local _W  = display.contentWidth
local _H  = display.contentHeight*.5
local _cW  = display.viewableContentWidth
local _cH  = display.viewableContentHeight
local myText, options, myTextData
local function onOrientationChange(e)
local stage = display.getCurrentStage()
    if e.type then
    local newAngle = stage.rotation - e.delta 
    transition.to(stage, {time = 1500, transition=easing.inOutQuad})
    stage.x  = _cW   * 0.5
    myText.x  = stage.x  * 0.5
    end   
    return true
end
myTextData = "Experience Hendrix LLC and Legacy Recordings, the catalog division of Sony Music Entertainment, will release, on DVD and Blu-ray , the expanded home video edition of the American Masters documentary Jimi Hendrix - Hear My Train A Comin' http://smarturl.it/JH_Train_amznBR, combined with a host of never-before-released special performance features, on Tuesday, November 5, 2013."
options =
{
    text  = myTextData,    
    x   = _W/2,
    y   = 0,
    width  = _W,
    font  = native.systemFontBold,  
    fontSize = 18,
    align  = "center"
}
myText = display.newText( options )
myText.anchorX = 0.5
myText.anchorY = 0
myText:setFillColor( 1, 1, 1 )
Runtime:addEventListener("orientation", onOrientationChange)

Maybe someone in here can help with the update of the textbox width so it fills out the display.viewableContentWidth

That would be optimale

Good luck damfodeky1

 

download the code here:

www.hideawayspa.no/hendrix/kurs/orinetationAndTextBox.zip




[topic_controls]
[/topic_controls]