A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Help converting game from AS2...

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Question Help converting game from AS2...

    ...to AS3.

    Hi, I am wondering if there is anyone who can help with the conversion of this code I need to convert it to AS3 but not sure what needs to be changed.
    All this code was on the character I was using.

    I hope someone can help.

    Code:
    onClipEvent (load) {
    	gravity = 10;
    	scale = _xscale;
    	walkSpeed = 10;
    	maxjump = 6;
    }
    onClipEvent (enterFrame) {
    	if (air == true) {
    		_y += gravity;
    		state = 3;
    	}
    	if (Key.isDown(Key.LEFT) && !_root.leftbound.hitTest(_x, _y, true)) {
    		_x -= walkSpeed;
    		_xscale = -scale;
    	}
    	if (Key.isDown(Key.RIGHT) && !_root.rightbound.hitTest(_x, _y, true)) {
    		_x += walkSpeed;
    		_xscale = scale;
    	}
    	if (_root.platforms.hitTest(_x, _y, true)) {
    		air = false;
    	} else {
    		air = true;
    	}
    	if (Key.isDown(Key.SPACE) && jump == true) {
    		_y -= jumpSpeed;
    	}
    	if (air == false) {
    		jump = true;
    		jumpcount = 0;
    		jumpSpeed = 22;
    	}
    	if (Key.isDown(Key.SPACE)) {
    		jumpcount += 1;
    	}
    	if (jumpcount > maxjump && jumpSpeed > -2) {
    		jumpSpeed -= 2;
    	}
    	if (air == false && !Key.isDown(Key.LEFT) && !Key.isDown(65)  or air == false && !Key.isDown(Key.RIGHT) && !Key.isDown(65)) {
    		state = 1;
    	}
    	if (Key.isDown(Key.LEFT) && air == false && !Key.isDown(65)   or Key.isDown(Key.RIGHT) && air == false && !Key.isDown(65)) {
    		state = 2;
    	}
    	if (!Key.isDown(65)) {
    		gotoAndStop(state);
    	}
    	_root.statetxt = state;
    }
    onClipEvent (keyUp) {
    	if (Key.getCode() == 83) {
    		jump = false;
    	}
    }

  2. #2
    Senior Member Sietjp's Avatar
    Join Date
    Jan 2005
    Location
    Paris, France
    Posts
    711
    http://livedocs.adobe.com/flex/2/langref/migration.html

    onXxxxxYyyyy has to be converted in this.addEventListener(Event.Zzzzzzz,method);
    Key.isdown doesn't exit anymore, you have to use a library (example http://code.google.com/p/bigroom/wiki/KeyPoll )

  3. #3

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Question

    Heya thanks for the links they shall come in handy

    So I was trying to just get the basics done first instead of doing the whole lot of code like above, this is what i've got so far.

    Code:
    var leftKeyDown:Boolean = false;
    var upKeyDown:Boolean = false;
    var rightKeyDown:Boolean = false;
    var downKeyDown:Boolean = false;
    var mainSpeed:Number = 15;
    
     
     shadow_mc.addEventListener(Event.ENTER_FRAME, moveChar);
    function moveChar(event:Event):void{
    
    	if(leftKeyDown){
    		shadow_mc.x -= mainSpeed;
    			shadow_mc.scaleX = -1;
    			
    	}
    	if(rightKeyDown){
    		shadow_mc.x += mainSpeed;
    			shadow_mc.scaleX = +1;
    			
    	}
    }
     
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
    function checkKeysDown(event:KeyboardEvent):void{
    
    	if(event.keyCode == 37 || event.keyCode == 65){
    		leftKeyDown = true;
    		shadow_mc.gotoAndStop("running");
    	}
    	if(event.keyCode == 38 || event.keyCode == 87){
    		upKeyDown = true;
    	}
    	if(event.keyCode == 39 || event.keyCode == 68){
    		rightKeyDown = true;
    		shadow_mc.gotoAndStop("running");
    	}
    	if(event.keyCode == 40 || event.keyCode == 83){
    		downKeyDown = true;
    			shadow_mc.gotoAndStop("duck");
    			
    	}
    }
    
    stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
    function checkKeysUp(event:KeyboardEvent):void{
    
    	if(event.keyCode == 37 || event.keyCode == 65){
    		leftKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 38 || event.keyCode == 87){
    		upKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 39 || event.keyCode == 68){
    		rightKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 40 || event.keyCode == 83){
    		downKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    }
    It seems to run left and right and duck but where the code is trying to load ("running") it only seems to run the frame once and not loop so my char just runs a bit then starts sliding across the floor.
    Any suggestions?

  5. #5
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    Just to clarify, on the frame named "running" is just your movieclip of a running animation that loops? And the problem is that this animation gets stuck on the first frame? Or last frame? Does this work fine in your AS2 version?

    I don't see any obvious problems with your code, it looks fine to me.

    I'd like to give you a tip, though. When adding eventListeners it's best to set the useWeakReference parameter to true (the last one). i.e.

    PHP Code:
    shadow_mc.addEventListener(Event.ENTER_FRAMEmoveCharfalse0true); 
    It's supposed to help with garbage collecting in AS3, so when you remove listeners they are cleared out for sure.

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Question Hi

    Hi all, thank you to those who have helped me so far, I have managed to sort out the running problem with this under the key down function:

    Code:
    		if(left || right)
    	{
    		if(walking ==false)
    		{
    			shadow_mc.gotoAndStop(2);
    			walking = true;
    		}
    Now my next problem is that I want to make it into a simple catching game but even hunting around I cant get much straight forward answers.
    I have a bit of code which makes the objects fall but I would prefer it to drop single objects but randomly, like typical catching games.

    Could anyone help with this please?

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Question Help Game Converting to AS3

    Hello im wondering if anyone could help me convert my old game code of AS2 to AS3, Im not expecting someone to do it for me (though it would be helpful :P ) just how I would go about doing it. Currently the code from AS2 was all on my main character but im looking to import my character from the library using a .AS in AS3. I've managed to convert the keyboard functions it's just mainly making platforms and the character to land on them.
    The code is as follows:

    Code:
    onClipEvent (load) {
    	gravity = 10;
    	scale = _xscale;
    	walkSpeed = 10;
    	maxjump = 6;
    }
    onClipEvent (enterFrame) {
    	if (air == true) {
    		_y += gravity;
    		state = 3;
    	}
    	if (Key.isDown(Key.LEFT) && !_root.leftbound.hitTest(_x, _y, true)) {
    		_x -= walkSpeed;
    		_xscale = -scale;
    	}
    	if (Key.isDown(Key.RIGHT) && !_root.rightbound.hitTest(_x, _y, true)) {
    		_x += walkSpeed;
    		_xscale = scale;
    	}
    	if (_root.platforms.hitTest(_x, _y, true)) {
    		air = false;
    	} else {
    		air = true;
    	}
    	if (Key.isDown(Key.SPACE) && jump == true) {
    		_y -= jumpSpeed;
    	}
    	if (air == false) {
    		jump = true;
    		jumpcount = 0;
    		jumpSpeed = 22;
    	}
    	if (Key.isDown(Key.SPACE)) {
    		jumpcount += 1;
    	}
    	if (jumpcount > maxjump && jumpSpeed > -2) {
    		jumpSpeed -= 2;
    	}
    	if (air == false && !Key.isDown(Key.LEFT) && !Key.isDown(65)  or air == false && !Key.isDown(Key.RIGHT) && !Key.isDown(65)) {
    		state = 1;
    	}
    	if (Key.isDown(Key.LEFT) && air == false && !Key.isDown(65)   or Key.isDown(Key.RIGHT) && air == false && !Key.isDown(65)) {
    		state = 2;
    	}
    	if (!Key.isDown(65)) {
    		gotoAndStop(state);
    	}
    	_root.statetxt = state;
    }
    onClipEvent (keyUp) {
    	if (Key.getCode() == 83) {
    		jump = false;
    	}
    }
    I hope someone can help

  8. #8
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    I was going to suggest searching the forum for "as3 convert" because someone just asked this and it turns out that it was your thread.

    http://board.flashkit.com/board/show...as3+convert%22

    Why start a new thread?

  9. #9
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Quote Originally Posted by Son of Bryce View Post
    I was going to suggest searching the forum for "as3 convert" because someone just asked this and it turns out that it was your thread.

    http://board.flashkit.com/board/show...as3+convert%22

    Why start a new thread?
    Well there two different things mainly, one was just character movement and the other is just to see if anyone can help with the conversion. It's easier to keep up with replys else I'll get reply for each one in the same thread.
    Im odd like that :P

  10. #10
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    PHP Code:
    package MainPackageName{

    import flash.events.*
    import flash.display.MovieClip;
    {
        public class  
    MainPackageName extends MovieClip
        
    {
            public function 
    MainPackageName():void {
                
    addEventListener(Event.ENTER_FRAMEOnEnterFrameFunction)
                
    stage.addEventListener(KeyboardEvent.KEY_DOWNkeyPress);
            }
            private function 
    OnEnterFrameFunction ():void {
                
            }
            private function 
    keyPress(EventVariable:KeyboardEvent):void {
                
    trace("keycode "+EventVariable.keyCode)
                
    //
                
    var TheCode:int EventVariable.keyCode
        
    }
        


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center