Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change the color of a sprite

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi you guys.... i have a question 4 u...

i have 2 cast members... 2 sprites.... let´s say circle1 and circle2

i can drag circle1 of them all over the stage... but.... here's my question...

can i change the color of circle2 when circle1 is touching it??

i really hope you guys can help me

THX
 
Yes, you can do that. There are some tutorials at on collision detection. The simplest example that is given there would be implemented as such:
on your frame script write:

on enterFrame
if sprite x intersects sprite y then
sprite y.foreColor=Z
else
sprite y.foreColor=original
end if
end
Y being the circle 2
x being moving circle1
Z being the number between 0 and 255 for the color change.

The are issues with this way though. It detects collision with the rectangle around the circle sprite and not just the shape. Again you may want to consult the tutorials at director-online for info on other techniques.
 
Thats the way i would do it. If you set the ink of both the sprites to "matte", though, INTERSECT will only return true if the actual circles intersect. And just if you don't like verbose, the INTERSECT command can also be writtten:

sprite(a).intersects(b)

Just in case you like that better ;-)
 
i did it...! thx both of you guys....

i have 1 more question...

i have 3 shapes.... circle1, square1 & square2...

circle1 is draggeable... the rest don't...

square1 is smaller and over square2... dont know if you guys follow me... is like looking from above to a table with a book over it...k?

so... when i drag circle1 over square2... it chage color.... and when is over square1 too.....

....but.... when circle1 is over square1.... also is over square2.... and both change their color.... but i dont want that....

what i want is...


if circle1 is over square1..... square2 doesn't change it's color.

a little bit complicated right?

i'm sorry guys.... but my english isn't good enough

but.....

I'm trying.... ;)


greetings
 
I believe this might solve that probelm. just write another frame script or add it to the other one.

on enterFrame
if sprite X intersects Y then
sprite Y.foreColor=A
else
sprite Y.foreColor=Original color
end if
if sprite X intersects Z then
sprite Z.foreColor=B
sprite Y.foreColor=Original color
end if
end

Sprite X as the circle
Sprite y being the bigger square and sprite z being the smaller square.

The first if then and else statement does the same thing as the code for the two circles interaction. But the if then statement is slightly different. On the then part of the statement I added another line for the Sprite Y to change back to its original color. So you move the circle over the big square and it changes color, but then changes back as soon as ithe circle moves over the smaller sqaure

 
hey you guys.... here with other question about the same thing.....

here's my situation....

i'm planning to do a table-game (don't know if i can say that way... but is something like monopoly)

there is a board with a lot of square's with numbers... and coin or something that shows where exactly each contestant is...

my coin is a little circle and my board is made of alot of square's...

so.... i have 5 circles for 5 contestants so.... when a circle is over a square... this change its color to the same color the circle is...

so i have to make this script.... is totally inefficient but is the best i can do....

on exitframe me
if sprite(101).intersects(7) then
sprite(7).color = rgb(255,0,0)
else
if sprite(102).intersects(7) then
sprite(7).color = rgb(0,255,0)
else
if sprite(103).intersects(7) then
sprite(7).color = rgb(255,255,0)
else
if sprite(104).intersects(7) then
sprite(7).color = rgb(0,0,255)
else
if sprite(105).intersects(7) then
sprite(7).color = rgb(128,128,128)
else
sprite(7).color = rgb(0,0,0)
end if
end if
end if
end if
end if
end

sprite 101-105 are my circles and sprite 7 is one of my square's....

here's my question....

do i have to copy-paste this script to all of my 62 square's that my board have???

i mean... is there a way to put something like...

if sprite(101).intersects with whatever then
(whatever).color = rgb(255,0,0)
else
(whatever).color = original
end if

and make 5 of this.... one for a diferent circle???

THX
 
1. Here some code for a solution:

property spriteNum

on mouseWithin
colorIntersect
end

on mouseLeave
colorIntersect
end

on mouseEnter
colorIntersect
end

on colorIntersect
if sprite(101).intersects(spriteNum) then
sprite(spriteNum).color = rgb(255,0,0)
else
if sprite(102).intersects(spriteNum) then
sprite(spriteNum).color = rgb(0,255,0)
else
if sprite(103).intersects(spriteNum) then
sprite(spriteNum).color = rgb(255,255,0)
else
if sprite(104).intersects(spriteNum) then
sprite(spriteNum).color = rgb(0,0,255)
else
if sprite(105).intersects(spriteNum) then
sprite(spriteNum).color = rgb(128,128,128)
else
sprite(spriteNum).color = rgb(0,0,0)
end if
end if
end if
end if
end if
end

I declared the spriteNum property and instead of refering to the actual sprite, use spriteNum. But you have to put this behavoir on each box sprite. Thats why the three handlers mouseWithin, mouseEnter, mouseLeave are refering to colorIntersect. The interaction seemed fine with this.The only thing is a box will not change back unless the circle hits another box, but if the game is similar to monopoly then your boxes are close together and should not have a problem with this. This could be all you need.

2 I also tried using the sendAllSprites property but it was problematic with a frame script. But if you want to try that it would go something like:

(the pertinent parts of your frame script)
property spriteNum

on enterFrame
colorIntersect
end

on colorIntersect
if sprite(101).intersects(spriteNum) then
sprite(spriteNum).color = rgb(255,0,0)
else
if sprite(102).intersects(spriteNum) then
sprite(spriteNum).color = rgb(0,255,0)
else
if sprite(103).intersects(spriteNum) then
sprite(spriteNum).color = rgb(255,255,0)
else
if sprite(104).intersects(spriteNum) then
sprite(spriteNum).color = rgb(0,0,255)
else
if sprite(105).intersects(spriteNum) then
sprite(spriteNum).color = rgb(128,128,128)
else
sprite(spriteNum).color = rgb(0,0,0)
end if
end if
end if
end if
end if
end

Then the script for your behavoirs to be placed on your boxes:

property spriteNum
property SendAllSprites

on beginSprite
sendAllSprites #colorIntersect
end

First, for some reason the frame script does not like the spriteNum (so there is an error when you try to run it) and secondly the beginSprite handler does not execute. So refer back to solution 1. Hope this helps out.
 
On more thing I need to amend that solution #1 from the earlier post:

use this instead for the behavoir script for the box sprites:

property spriteNum

on prepareFrame
colorIntersect
end

on colorIntersect
the rest as written in the earlier post


Instead of using mousEnter, mouseLeave and mouseWithin, which i just checked are more problematic than they should be. Use the prepareFrame handler. This change works better.
And incidentally, if you want to apply this behavoir to 62 sprites. I would go to the Behavoir Inspector while having all 62 selected. If they everything in common (such as having no behavoirs or all of the same behavoirs ) then you should be able to apply the behavoir to all of them at once. But this is something you might know already. Just trying to help out as best as i can.
 
Ya know whats a really cool way to do that code and impress all your friends? The REPEAT WITH and CASE commands are my fav ;-).

on colorIntersect
repeat with i = 101 to 105
if sprite(i).intersects(spriteNum) then
case i of
101: sprite(spriteNum).color = rgb(0,0,0)
102: sprite(spriteNum).color = rgb(30,60,70)
103: sprite(spriteNum).color = rgb(100,200,230)
104: sprite(spriteNum).color = rgb(1,20,50)
105: sprite(spriteNum).color = rgb(150,120,130)
end case
end if
end repeat
end

Just "neater" code I think. Does the same thing. I'm a neatness freak when it comes to proggramming. Just another example of how things can be written many ways. Good use of two powerful commands too!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top