开发者

Problem with Code

开发者 https://www.devze.com 2023-02-02 13:39 出处:网络
This may be a big ask, and I am just a beginner in coding - but can someone tell me what is wrong with this code? (it is fairly short.)

This may be a big ask, and I am just a beginner in coding - but can someone tell me what is wrong with this code? (it is fairly short.) The intentions of this code is to make the predefined object hero to move with acceleration and to slowly decelerate over time.

import flash.utils.Timer;
import flash.events.KeyboardEvent;

var vMove:Number = 0
var vMoveMaxL = -15
var vMoveMaxR = 15
var vLeft:Boolean = false
var vRight:Boolean = false
var vAccel:Number = 0.5
var vDeccel:Number = 1
var vDeccelFactor:Number = 0.1
var vTimer:Timer; new Timer(10)

vTimer.addEventListener(TimerEvent.TIMER, MovementTimer);
vTimer.start();

function MovementTimer(event:TimerEvent):void
{
 //Acceleration
 {
  //Initiation
  {
   stage.addEventListener(KeyboardEvent.KEY_DOWN, Acceleration);
   function Acceleration(event:KeyboardEvent):void
   {
    switch (event.keyCode)
    {
     case Keyboard.LEFT:
     {
      if  (vMove > vMoveMaxL)
      {
       vMove = vMoveMaxL;
       vLeft = true;
      }
      else
      {
       vMove -= vAccel;
       vLeft = true;
      }
     }
     case Keyboard.RIGHT:
     开发者_JAVA百科{
      if (vMove < vMoveMaxR)
      {
       vMove = vMoveMaxR;
       vRight = true;
      }
      else
      {
       vMove += vMoveMaxR;
       vRight = true;
      }
     }
    }
   }
  }
  //Blank
  {
   stage.addEventListener(KeyboardEvent.KEY_UP, Conditioner);
   function Conditioner(event:KeyboardEvent):void
   {
    switch (event.keyCode)
    {
     case Keyboard.LEFT:
     {
      vLeft = false;
     }
     case Keyboard.RIGHT:
     {
      vRight = false;
     }
    }
   }
  }
 }
 //Decceleration
 {
  if (vMove == 0)
  {
   vMove = 0;
  }
  else
  {
   if (vLeft==false && vRight==false)
   {
    vDeccel -= vDeccelFactor;
    {
     vMove *= vDeccel;
    }
   }
   else
   {
    vMove = vMove;
   }
  }
 }
 //Blank
 {
  hero.x += vMove;
 }
 //Blank
}


This line looks suspicious:

var vTimer:Timer; new Timer(10)

There are two statements there: the first says there's going to be a Timer named vTimer, the second creates a new Timer with a 10ms interval. I expect you meant for there to be a single statement which would create the Timer and assign it to vTimer. So it should be:

var vTimer:Timer = new Timer(10);

I haven't looked closely at the rest of the code but I hope this gets you on track.

edit: Looking more closely at the rest of your code I see quite a few issues.

Given the timer setup, vTimer will call MovementTimer every 10ms. 100 times a second. Inside each call to MovementTimer you're adding a new listener for KEY_DOWN, so after 1 second there will be 100 calls to Acceleration when you press a key.

You should move your key handler functions and listener assigments outside of your timer function. It looks like you're trying to implement a pattern where the key listeners set a flag (vLeft etc) and the timer applies acceleration on each tick. That's a good pattern but you have quite a few corrections to make before it will work.

Here's the basic structure I'd use for the listeners and key flags:

var left:Boolean = false;
var right:Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

function onKeyDown(event:KeyboardEvent):void {
  if (event.keyCode == Keyboard.LEFT) {
    left = true;
  }
  else if (event.keyCode == Keyboard.RIGHT) {
    right = true;
  }
}
function onKeyUp(event:KeyboardEvent):void {
  if (event.keyCode == Keyboard.LEFT) {
    left = false;
  }
  else if (event.keyCode == Keyboard.RIGHT) {
    right = false;
  }
}

var timer:Timer = new Timer(10);
timer.addListener(TimerEvent.TIMER, onTimer);

function onTimer(event:TimerEvent):void {
  if (left) {
    // do left acceleration things
  }
  else {
    // deceleration things
  }
  if (right) {
    // do right acceleration things
  }
  else {
    // deceleration things
  }
  // apply movements
}

I haven't tested this code but hopefully it's clear how it differs from your own code. After you have this working you should be able to tune your acceleration/deceleration with meaningful values again.

A couple of other things to note / think about:

  • Assigning a value to itself doesn't do anything, so you can remove the part where you say vMove = vMove
  • You use a lot of unnecessary blocks (the parts between { and }) around simple statements. In actionscript (and javascript) these don't change the scope of variables except in classes and class functions, so you can remove them unless they're needed for if/else statements.
  • your key up handler sets vLeft and vRight both to false. What happens if I press both the left key and the right key but I only release one of them?

Once you're happy with your code you should look around for a library to handle the key polling for you. A quick Google search suggests http://code.google.com/p/bigroom/wiki/KeyPoll would help you avoid spaghetti code when you want to support other key presses in the same way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号