4 Replies - 3343 Views - Last Post: 01 July 2013 - 05:28 AM

#1 fayn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-June 13

Why is the hit counter not adding up?

Posted 30 June 2013 - 10:17 AM

I'm trying to put on a scoring counter on this code, but the numbers doesn't add up...
I'm not sure if I'm placed the correct code or not. Why is it not working?
Please help me.



Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//Create an enemies array

var enemies:Array;
enemies = new Array();

// a counter that counts the current
// frame of the animation
var currentFrameCount:int;
// the total frames in the animation
var totalFrameCount:int = 20;
// the destination location 
var destinationX:Number = Math.random() * 450;
var destinationY:Number = 800;
// the location when the animation starts
var initialX:Number;
var initialY:Number;
// the distance between the initial
// location and the destination
var distanceX:Number;
var distanceY:Number;


//score
var hit:uint = 0;

makeEnemies();
moveEnemies();

//Call this function for how many enemies you want to make...
function makeEnemies():void
{
	var tempEnemy:MovieClip;
	//Make sure a Library item linkage is set to Enemy...
	tempEnemy = new Enemy();
	//tempEnemy.speed = 3;
	initialX = tempEnemy.x;
	initialY = tempEnemy.y;

	distanceX = destinationX - initialX;
	distanceY = destinationY - initialY;
	//tempEnemy.x = Math.round(Math.random() * 10);
	tempEnemy.cacheAsBitmapMatrix = tempEnemy.transform.concatenatedMatrix;
	tempEnemy.cacheAsBitmap = true;
	addChild(tempEnemy);
	enemies.push(tempEnemy);

	
	tempEnemy.addEventListener(MouseEvent.CLICK, killEnemies);
}






//Create an enemies array if you do not already have one...
//Create tempEnemy from Enemy library item if you do not already have one...
function moveEnemies():void
{
	var tempEnemy:MovieClip;
	for (var i:int =enemies.length-1; i>=0; i--)
	{
		tempEnemy = enemies[i];
		currentFrameCount++;
		if (currentFrameCount < totalFrameCount)
		{
			var progress:Number = currentFrameCount / totalFrameCount;
			tempEnemy.x = initialX + distanceX * progress;
			tempEnemy.y = initialY + distanceY * progress;
		}
		else
		{
			tempEnemy.x = destinationX;
			tempEnemy.y = destinationY;
			removeEnemy(i);
			trace("awwwww");
			MovieClip(this.root).gotoAndPlay(9);
		}
	}
}



function killEnemies(event:MouseEvent):void
{
	var tempEnemy:MovieClip;
	for (var i:int =enemies.length-1; i>=0; i--)
	{
		tempEnemy = enemies[i];
		removeEnemy(i);
	}
	hit += 1;
	trace("hits " + hit);
}

function removeEnemy(idx:int)
{
	removeChild(enemies[idx]);
	enemies.splice(idx,1);
}

function youLoose() {
	if (hit >= 5) {
		MovieClip(this.root).gotoAndPlay(1);
	}
	
}

function stopClip() {
	 removeEventListener(Event.ENTER_FRAME, moveEnemies);
}


I've put my hit/counter on line 85

Is This A Good Question/Topic? 0
  • +

Replies To: Why is the hit counter not adding up?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: Why is the hit counter not adding up?

Posted 30 June 2013 - 11:16 AM

If you can define in what way the numbers don't add up, it would help.
For example, if the hit counter is exactly double, or exponentially increasing, or only counting the blue aliens... Carefully understanding the problem is the first step in determining where to look.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: Why is the hit counter not adding up?

Posted 30 June 2013 - 11:29 AM

I'm a C# guy reading this... So its like reading code with an accent to me <laugh>

I don't see how the loop can really work correctly.

Line 88 - The .Length-1 is say 10 - and index i is 0
Then you delete index 0 at line 100 - making index 1 slide down to index 0
Then the loop continues and the enemies.Length-1 is now 99 but you move forward to index 1. Meaning the old 1, now in position 0, is never dealt with.
Basically you're skipping every other alien in the array.
If you are going to take away from the head of the array, you have to keep working index 0 until you have no aliens.


090        tempEnemy = enemies[0];
091        removeEnemy(0);

Was This Post Helpful? 0
  • +
  • -

#4 fayn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-June 13

Re: Why is the hit counter not adding up?

Posted 01 July 2013 - 12:13 AM

View PosttlhIn`toq, on 30 June 2013 - 11:16 AM, said:

If you can define in what way the numbers don't add up, it would help.
For example, if the hit counter is exactly double, or exponentially increasing, or only counting the blue aliens... Carefully understanding the problem is the first step in determining where to look.


Hi, thanks for replying. What I wanted to do is for numbers to add 1 point everytime the user clicks tempEnemy, like a scoring system. But instead of the numbers adding up together, it stays as.. 1.

Posted Image

That is the result I always get. Even when I moved the hit += 1; inside the for loop.

function killEnemies(event:MouseEvent):void
{
	var tempEnemy:MovieClip;
	for (var i:int =enemies.length-1; i>=0; i--)
	{
		tempEnemy = enemies[i];
		removeEnemy(i);
		hit += 1;
	}
	
	trace("hits " + hit);
}

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5537
  • View blog
  • Posts: 11,866
  • Joined: 02-June 10

Re: Why is the hit counter not adding up?

Posted 01 July 2013 - 05:28 AM

make line 8 (hit += 1); the very first line of the function.
Does it still fail to add on each click?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1