1 Replies - 1262 Views - Last Post: 29 September 2009 - 02:12 PM

#1 ProgrammerZach  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 23-July 09

If Key is pressed when objects are collided, something happens

Posted 29 September 2009 - 12:52 PM

New problem! I have a room with a cake. When you go to the cake, a box comes up saying press spacebar to make you blow out the candles. My current code is

stage.addEventListener(Event.ENTER_FRAME, cakeypoo)
function cakeypoo(h:Event) {
	if (hero.hitTestObject(cake)) {
		errormsg.text="Press [Spacebar] to blow out the candles!"
		
		}else{
		errormsg.text=""
		}
}



I was trying to use the

stage.addEventListener(Event.ENTER_FRAME, cakeypoo)
function cakeypoo(h:Event) {
	if (hero.hitTestObject(cake)) {
		errormsg.text="Press [Spacebar] to blow out the candles!"
		
		}else{
		errormsg.text=""
	}
	   if (Key.isDown(Key.SPACE)){
	   gotoAndPlay(5)
}



When i tried this it said that "key" is no longer supported and that i have to use something with keyboard. I might have to add an event listener somewhere, which i tried to no avail. I also tried to add one within an if statement which i figured would make it so if you are colliding with the cake you now have the option to press spacebar. Didnt work. I also tried to do an if statement within an if statement. as you can see, i definitely tried my best to figure it out on my own, but i have exhausted all other resources. any help would be appreciated! :D

Is This A Good Question/Topic? 0
  • +

Replies To: If Key is pressed when objects are collided, something happens

#2 ProgrammerZach  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 23-July 09

Re: If Key is pressed when objects are collided, something happens

Posted 29 September 2009 - 02:12 PM

Well, after more trial error and searching countlesss pages i have fixed my own problem! for anyone who needs it the solution was:

stage.addEventListener(Event.ENTER_FRAME, cakeypoo);
stage.addEventListener(KeyboardEvent.KEY_DOWN, downHandle)


function cakeypoo(h:Event) {
	if (hero.hitTestObject(cake)) {
		errormsg.text="Press [Spacebar] to blow out the candles!";

		} else {
		errormsg.text="";
		
	}
}


function downHandle(evt:KeyboardEvent):void {
	if (hero.hitTestObject(cake)) {
	if (evt.keyCode==Keyboard.UP) { 
		trace("hi");
	}
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1