small script from AS2 to AS3

  • rajaroy
  • Born
  • Born
  • rajaroy
  • Posts: 2

Post 3+ Months Ago

I am working on a puzzle game wich was created in a AS2 and i am trying to make it work on AS3.
Every puzzle piece has its own script and there are about 12 piece's.

The .FLA file
http://www.2shared.com/fadmin/5180059/b4b03dfc/jigsaw_puzzle_-_Copy.fla

http://i42.tinypic.com/jfb6n7.jpg

Script on every puzzle piece.
Code: [ Select ]
 
on (press) {
        this.swapDepths(_root.getNextHighestDepth());
        this.startDrag();
}
on (release) {
        stopDrag();
        if (eval(this._droptarget) == _parent.target4) {
                this._x = _parent.target4._x;
                this._y = _parent.target4._y;
                this._xscale = 100;
                this._yscale = 100;
                this.enabled = false;
                _parent.success++;
                if (_parent.success == _parent.no_piece) {
                        _root.gotoAndPlay("completed");
                }
        } else {
                this._x = random(230)+380;
                this._y = random(320)+80;
        }
}
 
  1.  
  2. on (press) {
  3.         this.swapDepths(_root.getNextHighestDepth());
  4.         this.startDrag();
  5. }
  6. on (release) {
  7.         stopDrag();
  8.         if (eval(this._droptarget) == _parent.target4) {
  9.                 this._x = _parent.target4._x;
  10.                 this._y = _parent.target4._y;
  11.                 this._xscale = 100;
  12.                 this._yscale = 100;
  13.                 this.enabled = false;
  14.                 _parent.success++;
  15.                 if (_parent.success == _parent.no_piece) {
  16.                         _root.gotoAndPlay("completed");
  17.                 }
  18.         } else {
  19.                 this._x = random(230)+380;
  20.                 this._y = random(320)+80;
  21.         }
  22. }
  23.  


I think there is something not right with the _root but not sure what to change it into.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

There are actually lots of differences. First of all you can't use code ON an object in AS3. So instead of the on(release) which is actually AS1 you would need to do a mouse action callback.

I would start by moving all the code to one frame in the timeline and then rewriting it from there.
  • rajaroy
  • Born
  • Born
  • rajaroy
  • Posts: 2

Post 3+ Months Ago

I am studying for webmaster as in PHP HTML CSS scripts not really for the actionscript. I cant figure it out with manuals and tutorials from the internet to make it properly work, even when i choosed such a simple gaming script.

Maybe you can help me with a little scripting to help me on the road to complete it? I know i am almost done but still to friggin complicated for me.
  • nornholdj
  • Novice
  • Novice
  • User avatar
  • Posts: 18

Post 3+ Months Ago

Code: [ Select ]
 
on (press) {
        this.swapDepths(_root.getNextHighestDepth());
        this.startDrag();
}
on (release) {
        stopDrag();
        if (eval(this._droptarget) == _parent.target4) {
                this._x = _parent.target4._x;
                this._y = _parent.target4._y;
                this._xscale = 100;
                this._yscale = 100;
                this.enabled = false;
                _parent.success++;
                if (_parent.success == _parent.no_piece) {
                        _root.gotoAndPlay("completed");
                }
        } else {
                this._x = random(230)+380;
                this._y = random(320)+80;
        }
}
 
  1.  
  2. on (press) {
  3.         this.swapDepths(_root.getNextHighestDepth());
  4.         this.startDrag();
  5. }
  6. on (release) {
  7.         stopDrag();
  8.         if (eval(this._droptarget) == _parent.target4) {
  9.                 this._x = _parent.target4._x;
  10.                 this._y = _parent.target4._y;
  11.                 this._xscale = 100;
  12.                 this._yscale = 100;
  13.                 this.enabled = false;
  14.                 _parent.success++;
  15.                 if (_parent.success == _parent.no_piece) {
  16.                         _root.gotoAndPlay("completed");
  17.                 }
  18.         } else {
  19.                 this._x = random(230)+380;
  20.                 this._y = random(320)+80;
  21.         }
  22. }
  23.  


Instead of using on(press) and on(release) you would want to add an event listener and listen for the MouseEvent.MOUSE_DOWN and MouseEvent.MOUSE_UP events and then create functions for each like so:
Code: [ Select ]
 
this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
 
function onMouseDown(e:MouseEvent):void
{
    //put on(press) code here
}
 
function onMouseUp(e:MouseEvent):void
{
    //put on(release) code here
}
 
  1.  
  2. this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  3. this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  4.  
  5. function onMouseDown(e:MouseEvent):void
  6. {
  7.     //put on(press) code here
  8. }
  9.  
  10. function onMouseUp(e:MouseEvent):void
  11. {
  12.     //put on(release) code here
  13. }
  14.  


Also if you want to be completely in AS3.0 you can get rid of all of the preceding underscores so:
_x = x (example: this._x now should be this.x)
_y = y
_parent = parent
_root = root
the _droptarget, _xscale, and _yscale I am unfamiliar with but I assume it follows the same guidelines.

The random(n) function should now use Math.random() which returns a random number between 0 and 1 which can be scaled from there by multiplying it by a number which would give you your range. This returns a decimal number however and would need to be rounded to give you an integer.

I believe the rest should work.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 14 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.