Skip to main content
October 20, 2015
Question

Passing scores to function, moving an enemy x & y

  • October 20, 2015
  • 2 replies
  • 254 views

Cut out irrelevant functions to make this shorter to read. How do I do what the 3 red comments say?

var sPoint:Point;

var bSpeed:Number = 5;

var radians:Number;

var degrees:Number;

var xDiff:Number;

var yDiff:Number;

var scoreAmount:Number = 0;

var curEnemy;

function moveObjects(evt:Event):void

{

  if(bulletContainer.numChildren > 0)

  {

  for(var b = 0; b <= bulletContainer.numChildren - 1; b++)

  {

  var tempBullet = bulletContainer.getChildAt(b);

  tempBullet.x += tempBullet.xmov;

  tempBullet.y += tempBullet.ymov;

  if(tempBullet.hitTestObject(curEnemy))

  {

  //call the updScore function and pass it the value of curEnemy.scoreValue

  bulletContainer.removeChild(tempBullet);

  enemyContainer.removeChildAt(0);

  createEnemy();

  }

  if(tempBullet.x < 0 || tempBullet.y < 0 || tempBullet.x > stage.stageWidth || tempBullet.y > stage.stageHeight)

  {

  bulletContainer.removeChild(tempBullet);

  }

  }

  }

  if(enemyContainer.numChildren > 0)

  {

  //using curEnemy move the curEnemy object along the x axis and the y axis, in other words change both it's x and y properties, you may want to check out the xmov and ymov properties of the curEnemy object

  if(curEnemy.hitTestObject(canonBase) || curEnemy.hitTestObject(canon))

    {

  //call the updScore function passing the NEGATIVE value of the curEnemy.scoreValue to it

  updScore(-curEnemy.scoreValue);

    enemyContainer.removeChild(curEnemy);

  createEnemy();

    }

  }

}

function createEnemy()

{

  var newEnemy:enemy = new enemy(); //creates new enemy

  radians = (Math.random() * 360) * Math.PI / 180; //calculates the radians variable

  newEnemy.x = canon.x + Math.cos(radians) * 300; //calculates x of enemy based on canon position

  newEnemy.y = canon.y + Math.sin(radians) * 300; //calculates y of enemy based on canon position

  newEnemy.rotation = (radians * 180 / Math.PI) - 180; //converts radians to degrees to calculate enemy rotation

  radians = Math.atan2(newEnemy.y - canon.y, newEnemy.x - canon.x); //finds new radians value based on the differences between newEnemy and canon x and y positions

  newEnemy.eSpeed = (Math.random() * 5) + 1; //calculates enemy speed based on a random value

  newEnemy.xmov = Math.cos(radians) * -1 * newEnemy.eSpeed; //calculates x movement with regards to eSpeed

  newEnemy.ymov = Math.sin(radians) * -1 * newEnemy.eSpeed; //calculates y movement with regards to eSpeed

  newEnemy.scoreValue = Math.floor(newEnemy.eSpeed * 10); //calculates value for value of current enemy

  enemyValue.text = String(newEnemy.scoreValue); //updates value of current enemy text field

  curEnemy = newEnemy; //makes current enemy new enemy

  enemyContainer.addChild(newEnemy); //adds new enemy child to enemy container

}

function updScore(sAmount)

{

  scoreValue.text = String(scoreAmount);

}

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
October 21, 2015

You are correct to convert the number to a string, but incorrect to use the wrong variable

function updScore(sAmount)

{

  scoreValue.text = String(sAmount);

}

Also, I would not count on passing " -variablename " and would instead multiply the variable by -1 as in

    updScore(-1*curEnemy.scoreValue);

or just add the "-" sign as text to the string in the function instead of passing it since it is not being calculated in any way and would appear to alweays be used based on hardc oding the -...

   scoreValue.text = "-"+String(sAmount);

Inspiring
October 20, 2015

Edit:

The function should be like this:

function updScore(sAmount:Number)

{

    scoreValue.text = String(sAmount);

}

To call updScore function:

updScore(-curEnemy.scoreValue); //updScore(theValueAsNumber);