13 Replies - 1558 Views - Last Post: 06 January 2015 - 02:12 PM

#1 JevinR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-December 14

Please help if/else statement

Posted 19 December 2014 - 10:33 AM

I am following this tutorial:

https://www.youtube....aCx2oI&index=31

Hello, I need help with this code:

stop();

import flash.events.Event;

stage.focus = this;

stage.addEventListener(KeyboardEvent.KEY_DOWN, pressedButton);
stage.addEventListener(Event.ENTER_FRAME, CollisionSensor);

var score:Number = 0; //Player Score
var enemy_velocity:Number = 5;
var objects:Array = [new RedBird(),new Goomba,new BlueBird(),new Ham()];

// objects[0] is a RedBird
// objects[1] is a Goomba
// objects[2] is a BlueBird
// objects[3] is an piece of Ham

var objectsIndex:Number = randomRange(0,3); //Randomizer
//Picks a random array value
for (var i:int=0; i<objects.length; i++)
{
objects[i].x = randomRange(0,3) * 50;
objects[i].y = -50;

//Brings objects to stage
stage.addChild(objects[i]);

}


//Generate a ranodom number from minNum to maxNum including the endpoints
function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}


function CollisionSensor(e:Event)
{

objects[objectsIndex].y = objects[objectsIndex].y + enemy_velocity;

// check if enemy objects have gone past the bottom of the game screen
if (objects[objectsIndex].y > stage.stageHeight)
{
objects[objectsIndex].y = -50; //Resets objects in array and parameters
// To enter frame in a repeated loop

//Randomizes a random x position for Objects
var num:Number = randomRange(0,3);
//This creates 4 random x positions - 0,50,100,150 To randomly place
//The array objects in
objects[objectsIndex].x = num * 50;

// now pick a random object
objectsIndex = randomRange(0,3);
// objects[0] is a RedBird
// objects[1] is a Goomba
// objects[2] is a BlueBird
// objects[3] is an piece of Ham


if(stage.frameRate <= 60)
{
stage.frameRate = stage.frameRate + 5;
}
else
{
stage.frameRate = 60;

if (enemy_velocity <= 25)
{
enemy_velocity += 1;
}
else
{
enemy_velocity = 25;
}
}
}

//check contanct with enemy objects

if((Pig.hit).hitTestObject(objects[objectsIndex]))
{
// Reset objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50
objects[objectsIndex].y = -50;

stage.removeEventListener(KeyboardEvent.KEY_DOWN,pressedButton);
stage.removeEventListener(Event.ENTER_FRAME,CollisionSensor);
stage.frameRate = 24;

gotoAndStop(1,"GG WP");
//trace ("you died!");
}
else if((Pig.hit).hitTestObject(objects[objectsIndex]) && objectsIndex == 3 )
{

score = score + 1; //increase score by 1 point
text1.text = String(score); // Score is now a string value
// Since text1 is a text box, it must be denotated as a string
// Reset the objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50;
objects[objectsIndex].y = -50
}
}


function pressedButton(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT && Pig.x >= 0)
{
Pig.x = Pig.x - 50;
}
else if (event.keyCode == Keyboard.RIGHT && Pig.x <= 100)
{
Pig.x = Pig.x + 50;
}
}



This particular snip-it of code is what doesn't compute:

if((Pig.hit).hitTestObject(objects[objectsIndex]))
{
// Reset objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50
objects[objectsIndex].y = -50;

stage.removeEventListener(KeyboardEvent.KEY_DOWN,pressedButton);
stage.removeEventListener(Event.ENTER_FRAME,CollisionSensor);
stage.frameRate = 24;

gotoAndStop(1,"GG WP");
//trace ("you died!");
}
else if((Pig.hit).hitTestObject(objects[objectsIndex]) && objectsIndex == 3 )
{

score = score + 1; //increase score by 1 point
text1.text = String(score); // Score is now a string value
// Since text1 is a text box, it must be denotated as a string
// Reset the objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50;
objects[objectsIndex].y = -50
}

I am trying to test for an array value, objectsIndex[3], in the if statement. When this value is tested, it checks if an object collides with a Pig object, and adds points accordingly.

I have included a snapshot the project in a test screen.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Please help if/else statement

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,076
  • Joined: 15-January 14

Re: Please help if/else statement

Posted 19 December 2014 - 11:21 AM

Quote

I am trying to test for an array value, objectsIndex[3], in the if statement.

"The" if statement? I don't know how many if statements are in that code, but there is more than one. Can you re-post the code using code tags so that it will highlight the code and put line numbers, and then say which line number you're referring to? Also, what's your question, what problem are you having?

This post has been edited by ArtificialSoldier: 19 December 2014 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: Please help if/else statement

Posted 19 December 2014 - 06:36 PM

If the screen shot is what you are working on, I would stop where you are now and not continue to use anything trademarked or copyrighted by someone else.
Was This Post Helpful? 0
  • +
  • -

#4 JevinR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-December 14

Re: Please help if/else statement

Posted 19 December 2014 - 08:46 PM

View PostArtificialSoldier, on 19 December 2014 - 11:21 AM, said:

Quote

I am trying to test for an array value, objectsIndex[3], in the if statement.

"The" if statement? I don't know how many if statements are in that code, but there is more than one. Can you re-post the code using code tags so that it will highlight the code and put line numbers, and then say which line number you're referring to? Also, what's your question, what problem are you having?


Sorry, I was referring to this particular if/else-if statement:

f((Pig.hit).hitTestObject(objects[objectsIndex]))
{
// Reset objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50
objects[objectsIndex].y = -50;

stage.removeEventListener(KeyboardEvent.KEY_DOWN,pressedButton);
stage.removeEventListener(Event.ENTER_FRAME,CollisionSensor);
stage.frameRate = 24;

gotoAndStop(1,"GG WP");
//trace ("you died!");
}
else if((Pig.hit).hitTestObject(objects[objectsIndex]) && objectsIndex == 3 )
{

score = score + 1;	//increase score by 1 point
text1.text = String(score); // Score is now a string value
// Since text1 is a text box, it must be denotated as a string
// Reset the objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50;
objects[objectsIndex].y = -50
}


I cannot seem to test the else if statement for value 4 in the array, (Which is objectsIndex[3]).
Was This Post Helpful? 0
  • +
  • -

#5 JevinR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-December 14

Re: Please help if/else statement

Posted 19 December 2014 - 08:52 PM

View Postastonecipher, on 19 December 2014 - 06:36 PM, said:

If the screen shot is what you are working on, I would stop where you are now and not continue to use anything trademarked or copyrighted by someone else.


Yeah, I figured making a cheap-knock off of a game would be easy. I'm not going to put a patent or do anything legitimately useful with this. Just a small project to learn some actionscript 3.

Can you figure out the if/else if statement?

f((Pig.hit).hitTestObject(objects[objectsIndex]))
{
// Reset objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50
objects[objectsIndex].y = -50;

stage.removeEventListener(KeyboardEvent.KEY_DOWN,pressedButton);
stage.removeEventListener(Event.ENTER_FRAME,CollisionSensor);
stage.frameRate = 24;

gotoAndStop(1,"GG WP");
//trace ("you died!");
}
else if((Pig.hit).hitTestObject(objects[objectsIndex]) && objectsIndex == 3 )
{

score = score + 1;	//increase score by 1 point
text1.text = String(score); // Score is now a string value
// Since text1 is a text box, it must be denotated as a string
// Reset the objects to y positions
objects[objectsIndex].x = randomRange(0,3) * 50;
objects[objectsIndex].y = -50
}


I'm trying to test the else if statement for value 4 in the array objectsIndex. I don't why it doesn't work, isn't

&& objectsIndex == 3 )


How you test for a value within a function?
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: Please help if/else statement

Posted 19 December 2014 - 08:55 PM

It looks like you are testing for a value of three, when you want either a range or a default statement instead.
Was This Post Helpful? 0
  • +
  • -

#7 JevinR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-December 14

Re: Please help if/else statement

Posted 19 December 2014 - 09:04 PM

View Postastonecipher, on 19 December 2014 - 08:55 PM, said:

It looks like you are testing for a value of three, when you want either a range or a default statement instead.


Yes! But how do you set a range for a specific array value?
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: Please help if/else statement

Posted 20 December 2014 - 09:28 PM

The general if statement with a range is:

if ( n >= 0 && n <= 100 ) // checks if range is between 0 and 100
 // Do this


The next thing is, what are you actually testing for,
(Pig.hit).hitTestObject(objects[objectsIndex]) or objectsIndex == 3?
Was This Post Helpful? 0
  • +
  • -

#9 JevinR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-December 14

Re: Please help if/else statement

Posted 21 December 2014 - 12:26 AM

View Postastonecipher, on 20 December 2014 - 09:28 PM, said:

The general if statement with a range is:

if ( n >= 0 && n <= 100 ) // checks if range is between 0 and 100
 // Do this


The next thing is, what are you actually testing for,
(Pig.hit).hitTestObject(objects[objectsIndex]) or objectsIndex == 3?


I am testing for

(Pig.hit).hitTestObject(objects[objectsIndex])


with a logical test for 3 which I tried as

(Pig.hit).hitTestObject(objects[objectsIndex]) && objectsIndex == 3 )


With this being the logical test

&& objectsIndex == 3 )


Thanks for replying, I tried posting to Adobe forums, but no one would help :/
Was This Post Helpful? 0
  • +
  • -

#10 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: Please help if/else statement

Posted 21 December 2014 - 02:49 PM

Then, the example in post #8 is what you are after.
Was This Post Helpful? 0
  • +
  • -

#11 JevinR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-December 14

Re: Please help if/else statement

Posted 03 January 2015 - 10:07 PM

View Postastonecipher, on 21 December 2014 - 02:49 PM, said:

Then, the example in post #8 is what you are after.


I tried doing this

if((Pig.hit).hitTestObject(objects[0]) && (Pig.hit).hitTestObject(objects[2]))
	   {
		  stage.removeEventListener(KeyboardEvent.KEY_DOWN,pressedButton);
		  stage.removeEventListener(Event.ENTER_FRAME,CollisionSensor);
		  stage.frameRate = 24;
		  
		  gotoAndStop(1,"GG WP");
		  //trace ("you died!");
	   }
	
	else((Pig.hit).hitTestObject(objects[objectsIndex]))
		 {
			 score = score +1; 
			 text1.text = String(score);
		 }


Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,076
  • Joined: 15-January 14

Re: Please help if/else statement

Posted 05 January 2015 - 03:13 PM

Quote

else((Pig.hit).hitTestObject(objects[objectsIndex]))

You forgot another if on that line.
Was This Post Helpful? 0
  • +
  • -

#13 JevinR   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-December 14

Re: Please help if/else statement

Posted 06 January 2015 - 12:04 PM

View PostArtificialSoldier, on 05 January 2015 - 03:13 PM, said:

Quote

else((Pig.hit).hitTestObject(objects[objectsIndex]))

You forgot another if on that line.


would it be else if?
Was This Post Helpful? 0
  • +
  • -

#14 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 8,076
  • Joined: 15-January 14

Re: Please help if/else statement

Posted 06 January 2015 - 02:12 PM

If you want to test that condition, yeah.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1