How I can create an function image visible and invisible

23 replies [Last post]
erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

HI
I need to make a function with objcet Timer but i don't know how to change the properties of a image(visible and invisible)

Thank you

Replies

sharp1959's picture
sharp1959
User offline. Last seen 19 hours 48 min ago. Offline
Joined: 28 Nov 2011

Hello erasmo.marciano,

here is some simple code to get you started....:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local ourBox = display.newCircle( 150, 150, 15 )
-- set starting properties of our object
ourBox.isVisable = false;
ourBox.alpha = 0;
 
local switchSeeMe = false;
 
-- print timer is running
local function printTimer()
 
        print("Timer Running!");
        
        -- we can also have our objects visable or invisable here
        
        if switchSeeMe == false then
        
                ourBox.isVisable = true;
                ourBox.alpha = 1;
                switchSeeMe = true;
                
        elseif switchSeeMe == true then
        
                ourBox.isVisable = false
                ourBox.alpha = 0;
                switchSeeMe = false;
        end
                
 
end
 
-- cancel timer
local function cancelTimer(e)
 
        if e.phase == "ended" then      -- up click from finger/mouse
        
                -- stop our timer
                timer.cancel(ourTimer);
                print("Timer Cancelled!");
        
        end
 
end
 
-- run our printtimer function every 2 seconds and loop forever
ourTimer = timer.performWithDelay(2000, printTimer, -1); -- ie: -1 = loop forever  10 = run 10 times
 
-- listen for a touch event on our screen
-- ourBox:addEventListener("touch", cancelTimer); -- use our object as a button to stop
Runtime:addEventListener("touch", cancelTimer);

Hope that will get ya going....study the code and you can
adapt it to what your looking for...:)

Happing Coding, have fun! ;)

Larry

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

Thank you

But my code don't work

local function showCalciatore(show)
local rand = math.random(3)
local IDmg=rand..".png"
local calciatore = display.newImage(IDmg)

if (show=="false")then
print(" show :" .. show)
calciatore.isVisible = false
end

if (show=="true")then
print(" show :" .. show)
calciatore.isVisible = true
end

calciatore.x = 50
calciatore.y = 220

end

I'm sure that " variable show" change the valure (true|false)

But don't work

I always see image.

:(

bejoy's picture
bejoy
User offline. Last seen 9 weeks 1 day ago. Offline
Joined: 17 Aug 2011

Another simple option is to move it out of screen by setting a x value.. e.g image.x = -100

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

I tried

local function showCalciatore(show)
local rand = math.random(3)
local IDmg=rand..".png"
local calciatore = display.newImage(IDmg)

if (show=="false")then
print(" show :" .. show)
calciatore.isVisable = false;
calciatore.x = -150
calciatore.y = -220

elseif (show=="true")then
print(" show :" .. show)
calciatore.isVisable = true;
calciatore.x = 50
calciatore.y = 220
end

end

BUT DON'T WORK

sharp1959's picture
sharp1959
User offline. Last seen 19 hours 48 min ago. Offline
Joined: 28 Nov 2011

Hi erasmo.marciano,

Post some code as to exactly what you want to do, and
I'll take a look at it....PLEASE use the code tags

listed below....

Makes is a lot easier to read your code..... :) :)

Thanks

Happy Coding ;)

Larry

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
local function showCalciatore(show)
   local rand = math.random(3)
   local IDmg=rand..".png"
   local  calciatore = display.newImage(IDmg)
 
   if (show=="false")then
         print(" show :" .. show)
         calciatore.isVisable = true;
         calciatore.x = -150
         calciatore.y = -220
 
    elseif (show=="true")then
         print(" show :" .. show)
         calciatore.isVisable = false;
         calciatore.x = 50
         calciatore.y = 220
    end
 
 
 
end
 
-----
 
 
local function onGlobalCollision( event )
        if ( event.phase == "began" ) then
    --  print( "Global report: " .. event.object1.nome .. " & " .. event.object2.nome .. " collision began" )
    if( event.object1.nome == "PUFFO" and  event.object2.nome =="bottonnew" )then
            redTotal = redTotal + 1
                updateRimbalzi()
 
    end
 
   if( event.object1.nome == "PUFFO" and  event.object2.nome =="birba" )then
            redTotal = redTotal - 1
                updateRimbalzi()
                media.playEventSound( "beep_mp3.mp3" )
 
----------LOOK HERE
            showCalciatore("true")
 
 
 
    end
 
    if( event.object1.nome == "PUFFO" and  event.object2.nome =="gargamella" )then
           redTotal = redTotal - 2
                updateRimbalzi()
                media.playEventSound( "fischio.mp3" )
     end
 
   if( event.object1.nome == "PUFFO" and  event.object2.nome =="PortaTop" )then
           redTotal = redTotal + 5
                updateRimbalzi()
                media.playEventSound( "Stadiook.mp3" )
 
----------LOOK HERE
 
               showCalciatore("false")
 
 
    end
 
 
        elseif ( event.phase == "ended" ) then
 
        end
end
 
Runtime:addEventListener( "collision", onGlobalCollision )

THANKS

jkrassman's picture
jkrassman
User offline. Last seen 6 hours 58 min ago. Offline
Joined: 4 Aug 2011

If you are using boolean variables - please remove the " around true or false.

if show == false then

if show then

Regards, Joakim

sharp1959's picture
sharp1959
User offline. Last seen 19 hours 48 min ago. Offline
Joined: 28 Nov 2011

Hello,

Joakim is correct, if using "true" vs true you will get
different results....

Try that and see if that works for ya... ;)

also, you might want to move the isVisable flag after you
move to x and y and set the alpha flag to false...

Let us know how is works for ya...or don't work for ya..

Thanks for putting it in the code tags... looks alot better. :)

Good luck...:)

Larry

alan120184
User offline. Last seen 1 week 13 hours ago. Offline
Joined: 16 Aug 2011

Although you would normally use true instead of true, his code actually checks for "true" anyway, so that shouldn't be affecting it.
In this part:

1
2
3
4
5
6
7
8
9
10
11
12
 if (show=="false")then
         print(" show :" .. show)
         calciatore.isVisable = true;
         calciatore.x = -150
         calciatore.y = -220
 
    elseif (show=="true")then
         print(" show :" .. show)
         calciatore.isVisable = false;
         calciatore.x = 50
         calciatore.y = 220
    end

you are setting isVisible to true when the x and y are off screen, and to false when it is on screen.

Should it not be the other way round:

1
2
3
4
5
6
7
8
9
10
11
12
 if (show=="false")then
         print(" show :" .. show)
         calciatore.isVisible = false;
         calciatore.x = -150
         calciatore.y = -220
 
    elseif (show=="true")then
         print(" show :" .. show)
         calciatore.isVisible = true;
         calciatore.x = 50
         calciatore.y = 220
    end

Also it could be a localisation thing but you had it spelt as 'isVisable' instead of 'isVisible'.

jkrassman's picture
jkrassman
User offline. Last seen 6 hours 58 min ago. Offline
Joined: 4 Aug 2011

Or why not,

1
2
3
4
5
6
7
8
9
10
11
12
 if (calciatore.isVisible==false)then
         print(" show :" .. show)
         calciatore.isVisible = true;
         calciatore.x = 150
         calciatore.y = 220
 
    else
         print(" show :" .. show)
         calciatore.isVisible = false;
         calciatore.x = -150
         calciatore.y = -220
end

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local function showCalciatore(show)
   local rand = math.random(3)
   local IDmg=rand..".png"
   local  calciatore = display.newImage("1.png")
          calciatore.x = 50
          calciatore.y = 220
 
     if (show=="false")then
         print(" show :" .. show)
             calciatore.isVisible = false;
 
      elseif (show=="true")then
         print(" show :" .. show)
         calciatore.isVisible = true;
     end
end

I tried as you suggested but don't work again.

Is there a method destroy? or clear?

Are you sure that objcet.isVisible = false|true works? ;

thank

jkrassman's picture
jkrassman
User offline. Last seen 6 hours 58 min ago. Offline
Joined: 4 Aug 2011

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local  calciatore = display.newImage("1.png")
       calciatore.x = 50
       calciatore.y = 220
           calciatore.isVisible = false
        
local function showCalciatore(value)
 
 
 
     if (value==false)then
         calciatore.isVisible = false;
      else
         calciatore.isVisible = true;
     end
end
 
showCalciatore(true)

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

TNKS

But also this method doesn't work

I'm going crazy :(

jkrassman's picture
jkrassman
User offline. Last seen 6 hours 58 min ago. Offline
Joined: 4 Aug 2011

You are kidding me, I just tested this and its nothing special - just straight forward code that works as it should.

Copy and paste this into a new project and put in the image and run it, i beat 100 bucks that it will work. There must be some other problems you are having. What are the console saying? Give me all the code you are having and I will sort it out for you.

Joakim

jkrassman's picture
jkrassman
User offline. Last seen 6 hours 58 min ago. Offline
Joined: 4 Aug 2011

Your syntax for isVisible is bad, you are using isVisable...theres nothing like that....

Joakim

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

I have coy and paste you code

http://www.giralatina.it/catalogo/gallery.tar my projcet

tnks

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

Hi

When I use isVisable don't work

I have Error attempt to concatenate local value

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

I'm sorry

There is Error because i Have write this

print(" show :" .. value)

Sorry again

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

GREAT NOW IT'S WORK

But there is a problem .....

I would that the value objcet calciatore is dynamic

1
 local  calciatore = display.newImage("1.png")

1
2
3
4
5
   
   local rand = math.random(3)
   local IDmg=rand..".png"
   local  calciatore = display.newImage(IDmg)
 

How I can do this?

Thank you

n.b

Now it's work with isVisible

Thank a lot

sharp1959's picture
sharp1959
User offline. Last seen 19 hours 48 min ago. Offline
Joined: 28 Nov 2011

Hello,

Hey my bad for the mis-spelling of isVisible...

copy and paste this in a new main.lua file
and run it...

see if I understand what your after....:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local ourBox = display.newCircle( 150, 150, 15 )
 
 
-- just to keep it simple we will use a table you also could
-- use a display.newGroup()
local IDmg = {};
ourBox.isVisible = false;
local switchSeeMe = false;
local rand = math.random;
 
 
 
IDmg[1] = display.newImage("1.png");
IDmg[1].isVisible = false;
IDmg[2] = display.newImage("2.png");
IDmg[2].isVisible = false;
IDmg[3] = display.newImage("3.png");
IDmg[3].isVisible = false;
 
-- set up our first image
local whichImage = IDmg[rand(3)];
 
 
local function showCalciatore(show)
   
   --local IDmg = rand..".png"
   --local  calciatore = display.newImage("1.png")
        
        if show == true then
                whichImage = IDmg[rand(3)];
                whichImage.x = 200;
                whichImage.y = 300;
        end
   
                
     if show == false then
       --  print(" show :" .. show)
                whichImage.isVisible = false;
 
      elseif show == true then
        -- print(" show :" .. show)
                whichImage.isVisible = true;
     end
         
         
         print("\nshow:   .. ",show);
         
         
end
 
 
 
 
 
 
 
-- print timer is running
local function printTimer()
 
        
 
        print("Timer Running!");
        --using your function here
        showCalciatore(switchSeeMe)
        
        
        if switchSeeMe == false then
        
                ourBox.isVisible = true;
                switchSeeMe = true;
                
        elseif switchSeeMe == true then
        
                ourBox.isVisible = false
                switchSeeMe = false;
        end
                
 
end
 
-- cancel timer
local function cancelTimer(e)
 
        if e.phase == "ended" then      -- up click from finger/mouse
        
                -- stop our timer
                timer.cancel(ourTimer);
                print("Timer Cancelled!");
        
        end
 
end
 
-- run our printtimer function every 2 seconds and loop forever
ourTimer = timer.performWithDelay(2000, printTimer, -1); -- ie: -1 = loop forever  10 = run 10 times
 
-- listen for a touch event on our screen
-- ourBox:addEventListener("touch", cancelTimer); -- use our object as a button to stop
Runtime:addEventListener("touch", cancelTimer);

Hope this time it may help you... ;)

EDIT: I think I posted this after you...got it to work..
man...I'm slow today... ;)

Happy Programming...

Larry

jkrassman's picture
jkrassman
User offline. Last seen 6 hours 58 min ago. Offline
Joined: 4 Aug 2011

Hi, I don't see the problem - it does randomly create a new image?

Joakim

sharp1959's picture
sharp1959
User offline. Last seen 19 hours 48 min ago. Offline
Joined: 28 Nov 2011

Hi,

You mean using physics...?

1
2
3
4
5
6
--top of your .lua file
local physics = require( "physics" );
physics.start();
 
local  calciatore = display.newImage("1.png")
physics.addBody( calciatore, "dynamic", { friction=0.5, bounce=0.3 } );

something like that....

Good luck...;)

Larry

erasmo.marciano
User offline. Last seen 5 days 17 hours ago. Offline
Joined: 27 Oct 2011

G R E A T ! ! !

Thank you a lot for your time

I reached my goal , thank at your help.

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.