dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [Help]man fly away from stage,AS3

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    13

    [Help]man fly away from stage,AS3

    hi guys,
    It has been 4 days, i m unable to find problem.I m quite annoyed by the performance of my player, actually it flies straight to up direction,without any keystroke when observed with simulate download, i m trying to create simple platform game where man(my player name) can jump , detect collision with walls & walk on top of bricks/platform like Mario

    Any Suggestions will be appreciated.
    Thanks in advance

    PHP Code:
    var arrow_left,arrow_right:Boolean;
    var 
    t:int=0;
    var 
    wal:Array;
    wal=new Array();
    var 
    X:Number;
    var 
    man:Object;
    var 
    Y:Number;
    var 
    gravity:Number=.004;
    var 
    lastTime:int;
    createman();
    this.addEventListener(Event.ENTER_FRAME,gameloop);
            function 
    createman()
            {
                
    man=new Object();
                
    man.mc=rect.man;// all my movieclips are inside rect
                
    man.dx=0.0;
                
    man.dy=0.0;
                
    man.jump=false;
                
    man.inAir=false;
                
    man.jumpspeed=.8;
                
    man.walkspeed=.6;
                
    man.mc.animstate="stand";
                
    man.direction=1;
                
    man.moveRight=false;
                
    man.moveLeft=false;
                
    man.walkAnimation = new Array(1,2,3,4,5,6,7,8,9);
                
    man.width=53.0;
                
    man.height=95.3;
                
    man.animstep=0;
                
    man.scaleX=.08;
                        
    man.scaleY=.08;
                
    man.x=-31.5;
                
    man.y=22;
                var 
    labels:Array=man.mc.currentLabels;
                for (var 
    i:uint 0labels.lengthi++)
                           {
                                 var 
    label:FrameLabel labels[i];
                                 
    trace("frame " label.frame ": " label.name);
                           }
            }
    stage.addEventListener(KeyboardEvent.KEY_DOWNdown);
    stage.addEventListener(KeyboardEvent.KEY_UPup);
    function 
    down(event:KeyboardEvent)
    {
        if(
    event.keyCode==37)
        {
    man.moveLeft=true;
        }
        else if(
    event.keyCode==39)
        {
    man.moveRight=true;
        }
        else if(
    event.keyCode==32)
        {   
        if (!
    man.inAir)
        
    man.jump=true;
        }
    }
    function 
    up(event:KeyboardEvent)
    {
        if(
    event.keyCode==37)
        {
    man.moveLeft=false;}
        else if(
    event.keyCode==39)
        {
    man.moveRight=false;}
    }
             
    count();
            function 
    count()
            { for (var 
    a:int=0a<this.rect.numChildrena++)
               {    
                   var 
    mc:Object=this.rect.getChildAt(a);
                   if (
    mc is wall)
                   { 
                               var 
    floorwall:Object=new Object();
                   
    floorwall.topside=mc.y;
                   
    floorwall.bottomside=mc.y+mc.height;
                   
    floorwall.leftside=mc.x;
                   
    floorwall.rightside=mc.x+mc.width;
                    
    wal[t]=floorwall;  
                     
    t++; 
                           }
                }
            }
            function 
    moveman(Char:Objecttimediff:int)
            {
                if(
    timediff<1)
                return;
                var 
    vertical:Number=Char.dy gravity*timediff;  
                if (
    vertical>15.0)
                
    vertical=15.0;
                    
    Char.dy+= timediff*gravity;
                    var 
    horizontalDist:Number=0;
                    var 
    newDirection:int=Char.direction;
                    var 
    newAnimstate:String="stand";
                    if (
    Char.moveLeft)
                    {
                        
    horizontalDist=-Char.walkspeed timediff;
                        
    newAnimstate="walk";
                        
    newDirection=-1;
                    }
                    else if(
    Char.moveRight)
                    {    
                        
    horizontalDist=Char.walkspeed*timediff;
                        
    newAnimstate="walk";
                        
    newDirection=1;
                    }
                     if(
    Char.jump)
                    {
                        
    Char.jump=false;
                        
    Char.dy=-Char.jumpspeed;
                        
    vertical=-Char.jumpspeed;
                        
    newAnimstate="jump";
                    }
                    
                    var 
    newY:Number=  Char.mc.vertical;
                    var 
    newX:NumberChar.mc.xhorizontalDist;
                    
    Char.hitWallLeft=false;
                    
    Char.hitWallRight=false;
                    
    Char.inAir=true;
        for (var 
    j:int=0;j<wal.lengthj++)
        {  
                if ((
    Char.mc.x+Char.width/>wal[j].leftside) && (Char.mc.x-Char.width/2<wal[j].rightside))   
                { 
                       if ((
    Char.mc.y  <= wal[j].topside)&& (newY>wal[j].topside))
                    { 
                        
    newY=wal[j].topside-Char.width;
                        
    Char.inAir=false;
                        
    Char.dy=0;
                        break;
                    }
                   }
                }
       for (var 
    j:int=0j<wal.lengthj++)
       {
         if((
    newY>wal[j].topside) && (newY-Char.height<wal[j].bottomside))
        {
         if (( (
    Char.mc.xChar.width/2) >=wal[j].rightside) && ((newX-Char.width/)<= wal[j].rightside))
            {    
                
    newX=wal[j].rightside Char.width/2;
                
    Char.hitWallLeft=true;
                break;
            }
             if(((
    Char.mc.x+Char.width/2)<=wal[j].leftside) && (newX +Char.width/2>=wal[j].leftside))
            {
                
    newX=wal[j].leftside-Char.width/2;
                
    Char.hitWallRight=true;
                break;
            }
        }                    
      }
                   
    Char.mc.x=newX;
                    
    Char.mc.y=newY;
                    if (
    Char.inAir)
                    {
                        
    newAnimstate="jump";
                    }
                    
    Char.animstate=newAnimstate;
                    if (
    Char.animstate == "walk"
                    {
                             
    Char.animstep += timediff/60;
                         
    trace(Char.animstep);
                         if (
    Char.animstep Char.walkAnimation.length)
                         {    
                        
    Char.animstep 0;
                         }
                       
    Char.mc.gotoAndStop(Char.walkAnimation[Math.floor(Char.animstep)]);
                    }
                    else 
                    {
                              
    Char.mc.gotoAndStop(Char.animstate);
                    }
                
                if (
    newDirection != Char.direction)
                {
                    
    Char.direction newDirection;
                    
    Char.mc.scaleX =-.08;
                }
            }
            function 
    gameloop(event:Event)
            {
                if (
    lastTime==0)
                
    lastTime=getTimer();
                var 
    timeDiff:int=lastTime-getTimer();
                
    lastTime+=timeDiff;
                
    moveman(man,timeDiff);
            } 
    Last edited by ash_at_risk; 11-24-2009 at 03:19 PM.

  2. #2
    Member
    Join Date
    Jun 2009
    Posts
    30
    hy, i have looked at ur code and tried to replicate the situation but im not getting the same errors as u lol, i getting different ones, will u be able to upload the fla or sumthing so i can a proper look?

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    13
    Thanks JT1,you tried
    finally i stumbled the cure of pain causing trouble, but this time arrive with new error, its probably concern with x-axis, actually my player stops certain distance before right wall and with left wall too,i tried to reduce the distance but end up in failure,i have attached the concern fla.

    The error associated with previous code:-
    Error:- var vertical:Number=Char.dy + gravity*timediff;
    Correct :-var vertical :Number=Char.dy*timediff + gravity*timediff;

    Inside loop of newY
    Error:-Char.dy=0;
    correct:-Char.dy=.7;

    Error:- Char.mc.scaleX =-.08;
    Correct:- Char.mc.scaleX =-.08*Char.direction;

    Error:- newY=wal[j].topside-Char.width;
    Correct:-newY=wal[j].topside-Char.width/4;

    Any more error diagnosis will be appreciated
    Attached Files Attached Files

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