Posts Tagged ActionScript 2

PrintJob problems in ActionScript 2

What I did mostly today was debug some ActionScript code on printing. All the basics about printing can be found in this tutorial. But as usual, what you want to do is more complicated than the basics. I have to talk a bit about my setup for you to understand the problem. What I usually do for a project, is that I load my content on 3 levels. The first level is the preloader which loads the shell (an external file) on level 2. The shell will load all the other content (also external files) on level 3. Also, because of all the rounded corners, content will be loaded under a mask.

So what I wanted to print was in the content MovieClip. I set out to do was in the tutorial, and everything worked really fine until I wanted to move the printArea MovieClip outside the visible screen (that’s the basic principle to do good printing using ActionScript). The big problem here is that content is under a mask so when I move the printArea out of the mask it become invisible so it won’t print. In order to solve that I had to attach the printArea MovieClip on the shell level (which is not under a mask). It sounds easy, but it took me 2 hours to find.

An other problem I had was that I was trying to offset a bit the stuff that I am printing so that when it prints there is a margin at the top and left. Well it seems that even if you offset (move a bit less and down) it doesn’t work, because for a reason, Flash start printing at the first pixel it encounters top and left. I think to circumvent that, I’ll put a single white pixel at 0, 0 so then my offset will work.

I hear there is also a problem with multiple page printing, but all my pages fit in one page so I did not have to solve that problem also

, ,

2 Comments


Porting a game in ActionScript 2 to ActionScript 3

Yesterday I blogged about a post on Emanuele Feronato’s blog. Today he put on is blog some code on an artillery game. It is nice code, but it is in ActionScript 2. I thought it would be fun to try to make the same thing in ActionScript 3. Here is the code from is post

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
Mouse.hide();
gravity = 2;
attachMovie("crosshair", "crosshair", 1);
attachMovie("tank", "tank", 2, {_x:230, _y:350});
crosshair.onEnterFrame = function() {
  this._x = _xmouse;
  this._y = _ymouse;
};
tank.onEnterFrame = function() {
  mousex = _xmouse-this._x;
  mousey = (_ymouse-this._y)*-1;
  angle = Math.atan(mousey/mousex)/(Math.PI/180);
  if (mousex<0) {
    angle += 180;
  }
  if (mousex>=0 && mousey>0) {
    angle += 360;
  }
  if (angle<160) {
    angle = 160;
  }
  if (angle<20) {
    angle = 20;
  }
  firepower = Math.sqrt(mousex*mousex+mousey*mousey);
  if (firepower>200) {
    firepower = 200;
  }
  this.cannon._rotation = angle*-1;
};
function onMouseDown() {
  angle = tank.cannon._rotation-1;
  start_ball_x = tank._x+48*Math.cos(angle*Math.PI/180);
  start_ball_y = tank._y+48*Math.sin(angle*Math.PI/180);
  cannonball_fired = attachMovie("cannonball", "cannonball_"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
  cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*firepower;
  cannonball_fired.diry = Math.sin(angle*Math.PI/180)*firepower;
  cannonball_fired.onEnterFrame = function() {
    this.diry += gravity;
    this._x += this.dirx/50;
    this._y += this.diry/50;
    if (this._y+this._height/2>350) {
      this._y = 350-this._height/2;
      this.diry = -this.diry*.3;
    }
  };
}

This code is on the first frame of the fla file. To transfer this in ActionScript 3 and in an object oriented way (I am just starting to learn object oriented programming so if you see way I could improve the code just tell me so). In order to do so I divided his code into 4 classes. One class for the document class, one class for the crosshair (I don’t think I needed to make this as a class but it seemed like a good way to divide things), one class for the tank and one class for the cannonball. Here is the code for the document class saved in a file called artillery9AS3.as since my fla is named artillery9AS3.fla:

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
 
package{
  import flash.display.Sprite;
  import flash.ui.Mouse;
  import flash.events.*;
  import artillery.*;
  public class artillery9AS3 extends Sprite{
    private var gravity:int;
    private var firepower:Number;
    var crosshair:Crosshair;
    var tank:Tank;
    public function artillery9AS3(){
      Mouse.hide();
      gravity = 2;
      crosshair = new Crosshair();
      addChild(crosshair);
      tank = new Tank(230, 350);
      addChild (tank);
      stage.addEventListener(Event.ENTER_FRAME, doEnterFrame);
      stage.addEventListener(MouseEvent.MOUSE_DOWN, doMouseDown);
    }
    private function doEnterFrame(e:Event){
      crosshair.setPos(mouseX, mouseY);
      var newMousex:Number = mouseX-tank.x;
      var newMousey:Number = (mouseY-tank.y)*-1;
      var newAngle:Number = Math.atan(newMousey/newMousex)/(Math.PI/180);
      if (newMousex<0) {
         newAngle += 180;
      }
      if (newMousex>=0 && newMousey>0) {
        newAngle += 360;
      }
      if (newAngle>160) {
         newAngle = 160;
      }
      if (newAngle<20) {
        newAngle = 20;
      }
      tank.setAngle(newAngle);
      firepower = Math.sqrt(newMousex*newMousex+newMousey*newMousey);
      if (firepower>200) {
        firepower = 200;
      }
    }
    private function doMouseDown(e:MouseEvent){
      var angle:Number = tank.getAngle() - 1;
      var start_ball_x:Number = tank.x + 48*Math.cos(angle*Math.PI/180);
      var start_ball_y:Number = tank.y + 48*Math.sin(angle*Math.PI/180);
      var dirX:Number = Math.cos(angle*Math.PI/180)*firepower;
      var dirY:Number = Math.sin(angle*Math.PI/180)*firepower;
      addChild(new Cannonball(start_ball_x, start_ball_y, dirX, dirY, gravity));
    }
  }
}

Mostly what I am doing here is three things, I create the tank and the crosshair, I add the ENTER_FRAME and the MOUSE_DOWN listeners and I handle these events. In the ENTER_FRAME handler I give the new position to the crosshair, and the new angle to the tank. In the MOUSE_DOWN handler, I create a new cannonball and I give it its parameters so that it can update itself alone.

The code for the crosshair and the tank is pretty straight forward so I won’t paste it here, but download the zip file if you want to see it.

Here is the code for the cannonball

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
 
package artillery{
  import flash.display.Sprite;
  import flash.events.*;
  public class Cannonball extends Sprite{
  private var dirX:Number;
  private var dirY:Number;
  private var gravity:Number;
  public function Cannonball(xpos:Number, ypos:Number, newDirX:Number, newDirY:Number, newGravity:Number){
    x = xpos;
    y = ypos;
    dirX = newDirX;
    dirY = newDirY;
    gravity = newGravity;
    addEventListener(Event.ENTER_FRAME, doEnterFrame);
  }
  private function doEnterFrame(e:Event):void{
    dirY += gravity;
    x += dirX/50;
    y += dirY/50;
    if (y+ height/2 >350) {
      y = 350- height/2;
      dirY = - dirY * .3;
    }
    if (x < 0)
    {
       removeEventListener(Event.ENTER_FRAME, doEnterFrame);
       this.parent.removeChild(this);
    }
    }
  }
}

This code is saved in a file named Cannonball.as in the folder artillery. Basically it updates its position based on the direction it had was it was created. I modified one thing in this code. When the cannonball x position is less than, I remove the ENTER_FRAME listener and I remove this cannonball from the display list. I do this because, for one the cannonball is not visible anymore so we should not calculate its position. Also, with the previous code, all the cannonballs that you had shot would continue to use up memory, so if you played for a long time, you would eventually run out of memory. By removing the listener and the display object, the garbage collector will remove the unused cannonballs thereby freeing memory.

Also, I mostly used Sprites since there was no timelines involved (the cannon is still a MovieClip, I didn’t take the time to convert it). This is good for the crosshair and the tank, but I think the cannonballs should have stayed a MovieClips since eventually you will want these cannonballs to explode and use a timeline in order to animate the explosions.

Well that is about it for now, feel free to download the files to look at the source code.

Source of the artillery game

, ,

2 Comments


9-slices scaling, Papervision 3D, Tweener and FlashDevelop

I am currently working on a website with tons of rounded corners, so I’m pretty happy they added 9-slices scaling in Flash 8. The only problem I had today, was that I wanted to apply 9-slices scaling on a mask and it happens that you just can’t. You can’t do it using ActionScript and you can’t use it using the Flash Authoring tool. I don’t know about flash 9, but I sure hope they will get that to work.

I also stumbled upon something that looks pretty cool: Papervision 3D . The name says it, you can use it to do 3D in flash, haven’t tried it yet but it looks sweet. And while browsing their website I found a nice example using Tweener which I spoke off recently. They’ll both deserve more investigation.

Finally, on the same project I am working with all those rounded corner I am using FlashDevelop. I use it because I find the Flash Authoring tool to be really bad (Flash 8 ) for ActionScript. Well I said FlashDevelop was good, but I would rectify what I said, and now say that it is still experimental. I had all sort of problems with it, mostly with the auto-completion tool completing things all the time which I didn’t ask for . Just thought I’d share that with you.

, , , ,

No Comments


2 new tutorials and SEO

I have been busy this weekend, I have made two new tutorials about how to time your animations, one in ActionScript 2, one in ActionScript 3. I hope this help.

If you noticed there is the word SEO (search engine optimization) in the title of the blog but I didn’t have the time to write about it yet. In the meanwhile I can direct you to the website the-flash-files made by Nurun , a web agency in Montreal (actually a competitor of the company I work for).

, , , ,

1 Comment


Flash Media Server: loading FLV and onMetaData problems

I kinda like having problems at job now, because it gives me something to write about. I had two big problems today. We switched from a traditional server to a Flash Media Server to host our videos. So I had to adapt the actionScript to connect to the new server. I thought this would be a piece of cake; so wrong I was. All I wanted to do is change the address in the link in the netStream.play method. Nope, the thing with Flash Media Server is that you have to connect to the application first and then request the file to stream. I did just that like indicated everywhere, but what nobody will tell you, like if it was a big secret, it’s that you don’t put the extension of your movie in its link so insteand of “design/intro_movie.flv”, you have to do it this way: “flv:design/intro_movie”. Quite strange and unintuitive way to do. Anyway, once you get this figured out, it’s pretty easy, but still I had to experiment for about 3 hours just to find that out. I’ll post the code for the connection tomorrow.

, , ,

2 Comments