Creation of realistic spheres in Flash with textures and masking

I was preparing a new tutorial involving a visual from above, with a ball rolling on a tile map, and that's what I coded first, applied to an object linkaged as "ball" in the first frame:

ACTIONSCRIPT:
  1. power = 0.3;
  2. yspeed = 0;
  3. xspeed = 0;
  4. friction = 0.95;
  5. _root.attachMovie("ball", "ball", 1, {_x:250, _y:175});
  6. ball.onEnterFrame = function() {
  7.     if (Key.isDown(Key.LEFT)) {
  8.         xspeed -= power;
  9.     }
  10.     if (Key.isDown(Key.RIGHT)) {
  11.         xspeed += power;
  12.     }
  13.     if (Key.isDown(Key.UP)) {
  14.         yspeed -= power
  15.     }
  16.     if (Key.isDown(Key.DOWN)) {
  17.         yspeed += power
  18.     }
  19.    
  20.     xspeed *= friction;
  21.    
  22.     this._y += yspeed;
  23.     this._x += xspeed;
  24. };

This code is already explained in this tutorial, and it's almost the same except it has no gravity since the "camera" is above the objects.

This is the result:

If you play with arrow keys you should be able to have a sphere rolling through the stage.

I wrote that "should" because you... should be able to see the sphererolling... but the only thing you really see is a red circle. I can't see any sphere.

A sphere should be more... let's say... spherical... so I played with gradients and that's what I got:

This is more likely a sphere, but it seems to float in the air. A sphere on a plane should drop a shadow.

I need a shadow.

Very easy, here it is the sphere with the shadow...

Now you should see a rolling sphere. I only see a skating sphere. A very polished sphere, moreover.

Time to add some tricks.

I want to map a texture on the sphere to make it looks like a marble sphere, or wooden sphere, or whatever.

So I need a seamless texture.

Seamless textures are textures that form a continuous texture when you tile the image over and over.

I will explain in a future tutorial how to create seamless textures, at the moment I googled for "samless texture" and I found those free images. Do the same, and be sure you are saving squared seamless textures.

I liked those 3 textures, and scaled down to 188x188 pixels since the circle diameter in my movie is 60 so the circumference is 60*Pi = 188

Wooden texture
wooden texture

Paper texture
paper texture

Tiger texture
tiger texture

With the tiger texture, I imported it into the movie and linkaged all in this way:

Linkage

ball: it's the final ball, combining all objects

ball_itelf: it's the ball itself, I mean the blue circle

texture: it's the texture I imported

Now you should have a movie like this one:

The texture moves with the ball.

Next step will be adding a mask

ACTIONSCRIPT:
  1. power = 0.3;
  2. yspeed = 0;
  3. xspeed = 0;
  4. friction = 0.95;
  5. _root.attachMovie("ball", "ball", 1, {_x:250, _y:175});
  6. ball.texture.setMask(ball.ball_itself);
  7. ball.onEnterFrame = function() {
  8.     if (Key.isDown(Key.LEFT)) {
  9.         xspeed -= power;
  10.     }
  11.     if (Key.isDown(Key.RIGHT)) {
  12.         xspeed += power;
  13.     }
  14.     if (Key.isDown(Key.UP)) {
  15.         yspeed -= power
  16.     }
  17.     if (Key.isDown(Key.DOWN)) {
  18.         yspeed += power
  19.     }
  20.    
  21.     xspeed *= friction;
  22.    
  23.     this._y += yspeed;
  24.     this._x += xspeed;
  25. };

Line 6: A mask with the shape of ball_itself is assigned to the object texture.e its own mask, for example mc.setMask(mc) . Masking is a very useful technique in Flash. By applying a mask on a movie clip, you can restrict the visible area of the target clip to the mask area. In this case, I restrict the visible area of the texture movieclip according to ball_itself movieclip shape.

This is the result

We have a textured circle (not a sphere yet).

In a real sphere, the texture would roll together with the sphere.

That's where we need another trick.

With your favourite photo editor you have to open your 188x188 images, double their x and y sizes (376x376) and fill them with old 188x188 texture. You should have an image tiled with 4 of your textures, like in this examples:

Wooden texture
wooden texture

Paper texture
paper texture

Tiger texture
tiger texture

Tiger texture
tiger texture with lines marking the tileset made with the old tiger texture

Now, replace the old texture in the actionscript with this new one, and add those lines:

ACTIONSCRIPT:
  1. power = 0.3;
  2. yspeed = 0;
  3. xspeed = 0;
  4. friction = 0.95;
  5. _root.attachMovie("ball", "ball", 1, {_x:250, _y:175});
  6. ball.texture.setMask(ball.ball_itself);
  7. ball.onEnterFrame = function() {
  8.     if (Key.isDown(Key.LEFT)) {
  9.         xspeed -= power;
  10.     }
  11.     if (Key.isDown(Key.RIGHT)) {
  12.         xspeed += power;
  13.     }
  14.     if (Key.isDown(Key.UP)) {
  15.         yspeed -= power;
  16.     }
  17.     if (Key.isDown(Key.DOWN)) {
  18.         yspeed += power;
  19.     }
  20.     xspeed *= friction;
  21.     yspeed *= friction;
  22.     this._y += yspeed;
  23.     this._x += xspeed;
  24.     this.texture._y += yspeed;
  25.     this.texture._x += xspeed;
  26.     if (this.texture._x>158) {
  27.         this.texture._x -= 188;
  28.     }
  29.     if (this.texture._x<-158) {
  30.         this.texture._x += 188;
  31.     }
  32.     if (this.texture._y>158) {
  33.         this.texture._y -= 188;
  34.     }
  35.     if (this.texture._y<-158) {
  36.         this.texture._y += 188;
  37.     }
  38. };

Lines 24-25: The texture already moves with the ball, but I want it to move twice faster of ball's x and y speeds to simulate the effect of a rolling texture.

Lines 26-37: If the position of the texture relatively to the ball one is more than 158 (texture width/2 - ball radius) then make or less than -158, then adjust texture position shifting it by 188 pixels left, right, up, or down. The player won't realize the texture jumped because it is tiled and seamless.

Now, you should see a rolling sphere. What is see is a moving circle with a smart use of a texture sliding across the object.

In a real sphere, the perspective modifies texture appearance.

To achieve this, we'll need a displacement map. I will cover in a future tutorial.

Meanwhile, look what you can do adding a level with some shadows:

Download all the source codes and give me feedback

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5 out of 5)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

39 Responses to “Creation of realistic spheres in Flash with textures and masking”

  1. abhilash on June 10th, 2007 11:10 am

    Very cool!
    the last example was look so cool!

  2. Michael on June 10th, 2007 4:41 pm

    Wow, very nice. I always wondered how to mimic 3D textures with movement.
    The last example is cool indeed.

  3. Frederik J on June 10th, 2007 5:15 pm

    Hi Emanuele. Its very nice, i love it!
    If you want some criticism, here it is:

    What i think you should do, to get it look more realistic, is, to zoom the texture in the middle a little bit.
    Thats because when you see a sphere the “texture” will be flatter and flatter, the more closer it is to the circumference.
    //Frederik

  4. Virtualism on June 11th, 2007 1:39 pm

    What the hell? as soon as i click this i get 10 different exploit warnings about stuff in my temporary internet files, it asked me 3 times to run an active X thingy. are you trying to hack me?

  5. Jerry on June 11th, 2007 5:58 pm

    Very nice.

  6. Monkios on June 12th, 2007 7:49 pm

    After seing this, I think it’d be pretty cool to have a tutorial on how to make shadows.

    I guess with some trigonometry I could get it working on this.

  7. Robert Penner on June 12th, 2007 10:59 pm

    > the circle diameter in my movie is 60*Pi = 188

    The diameter is 60; the circumference is 188.

  8. Create a Flash ball game with visual from above tutorial at Emanuele Feronato on June 14th, 2007 12:49 pm

    [...] Line 22: Attaching the ball. I created the ball in the same way as in Creation of realistic spheres in Flash with textures and masking tutorial. [...]

  9. lewis on June 22nd, 2007 8:44 pm

    HELP! Ican’t do the bit before the mapping it goes dodgy HELP!

  10. Jerm on June 23rd, 2007 2:59 am

    everything works fine except the texture wont be attached to the ball, it might be because I’m using flash MX. if anyone knows how to help, please tell me. Thanks

  11. SC@VENGER on June 26th, 2007 7:45 pm

    Er, in Flash MX 6.0 the sources don’t open (Unexpected file format). Which version of Flash do I need?

  12. Vince on July 7th, 2007 1:44 am

    ive heard of action script before. and im interested to learn some :)

  13. Flash Prayer on July 11th, 2007 12:52 pm

    Great tutorial.

    I will make a post about it at http://www.beedigital.net/blog

    Great stuff…

  14. TutorialsNets » Como hacer una bola realista con Flash on July 23rd, 2007 3:13 am

    [...] Estube viendo un tutorial en http://www.emanueleferonato.com, me parecio muy interesante. [...]

  15. Create a Flash ball game with visual from above tutorial part 2 at Emanuele Feronato on August 3rd, 2007 6:34 pm

    [...] Line 3: Masking the ball as explained in Creation of realistic spheres in Flash with textures and masking. [...]

  16. jamie on August 9th, 2007 11:51 pm

    great look. i have a seriouse problem with the texture. i dont believe i linkeged the objects correctly and im abit confused about what each symbol consists of.. plz help

  17. Croupier on August 31st, 2007 5:00 am

    I couldn’t get thie mask code to work // ball.texture.setMask(ball.ball_itself);
    //

    So I just deleted ball_itself mc, added a mask to layer 1 in ball mc and deleted the code, works nice now.

  18. Easy Only! Glog » Blog Archive » Ball Physics on September 8th, 2007 2:01 am

    [...] Johan van Mol on bouncing balls off vectors Micheal Battle on bouncing balls off other balls Emanuele Feronato on texture mapping on balls [...]

  19. Create a survival horror game in Flash tutorial - part 1 : Emanuele Feronato - blog of an italian geek on September 26th, 2007 8:49 pm

    [...] you want to know more about masking, read Creation of realistic spheres in Flash with textures and masking. PLAIN TEXT [...]

  20. s0d4player on October 9th, 2007 4:38 am

    Virtualism, Emanuele isn’t hacking you. It’s just that Internet Explorer is too secure in the wrong areas. SWFs are probably thought as a threat to IE. Use Firefox if you don’t want these unnecessary security alerts for things like just swfs or harmless javascript.

  21. s0d4player on October 9th, 2007 5:25 am

    BUG, you forgot to implement friction on yspeed in some of the examples.

  22. Amal on October 9th, 2007 2:39 pm

    Hi,
    I liked your turorial its very helpfull.

    Can you help me
    In my flash I am masking an object with a texture which is called dynalically through XML

    Here the texture is get loaded but its not masking the object

    What can I do for this ?

    Thank You for reading this , Hope you will help me in this.

  23. joshmandic on October 30th, 2007 1:55 am

    if i wanted to make this into a ball for pong how would i go about this

  24. Neil on November 2nd, 2007 3:49 pm

    how do i make the shadows?? can any1 link a tutorial for it. thanks

  25. tommy on November 24th, 2007 8:48 am

    hey i have a quiestion

    i would really know how to do that masking thing im new i have no idea how to mask my ball_itself

    if would be really cool if you can tell me how

    thanks for reading this

  26. emanuel on November 30th, 2007 8:02 pm

    hi i im making a pong game can u tell me how to use masking with that?

  27. Understanding Flash displacement map filter : Emanuele Feronato - italian geek and PROgrammer on December 3rd, 2007 6:34 pm

    [...] is a standalone tutorial but it’s in some way connected with the Creation of realistic spheres in Flash with textures and masking [...]

  28. Raidation on February 7th, 2008 6:54 am

    Hi! Nice tut! I’m having trouble with this. When I’m rolling the ball around, something in the background just makes the ball disappear. Can you help me with this?

  29. evandro on February 15th, 2008 7:41 pm

    I didn’t see any movement!!!

  30. evandro on February 15th, 2008 7:43 pm

    Damm iT!!!! now I Saw!!!!!!! Thats great!

  31. Alex on February 18th, 2008 2:16 am

    Im looking to make somthing like a tennis ball, what would the backround texture look like? any ideas?

  32. Grex on March 6th, 2008 7:42 pm

    Hi Emanuele. I really liked this tutorial but I can’t make the masking working??? When I test the movie the texture moves with the ball but like a square again. Any ideas??? Thanks in advance!!!

  33. roboman on April 12th, 2008 1:29 pm

    man, this is so cool!

  34. bakironnus on May 29th, 2008 3:52 pm

    hi Grex , may be you have to make an instane name for both texture and ball_itself movie clips wich are located in ball mc.

  35. denzoo on May 30th, 2008 2:20 pm

    Hey I used your tutorial to create a game, you can check it out here: http://www.newgrounds.com/portal/view/441888

    Thanks a lot for sharing your ideas.

  36. madpassion on June 12th, 2008 3:42 am

    If i wanted to make the letter A move the ball left, what would be the actionscript?

    Left is

    if (Key.isDown(Key.LEFT)) {
    xspeed -= power;
    }

    what would be A?

  37. Corey on June 26th, 2008 2:25 am

    I have never seen this technique before and I have to say it is amazingly clever — so convincing yet so simple! Thank you very much for the inspiration!

  38. raoni on August 9th, 2008 3:04 pm

    como eu faso para min gria um jogo

  39. Nemesis on August 19th, 2008 6:08 pm

    Cool tutorial i got it to work, but it took a few hours, longer than it should have. I think you need to add more details but im just starting out so maybe its not your fault. Anways its all good but i want to add a hitTest to the ball but i cant get it to work. how do i have the ball,the light, texture , and its shadow all stop when it hits a barrier. Thanks in advance.

Leave a Reply