jagarock
11-27-2009, 03:16 PM
the problem:
the CTRL button in this game is doing two things:
if movieclip is visible, then turn it off.
if movieclip is not visible, then turn it on.
however, if i hold the CTRL button pressed, it will trigger over and over, turning the movieclip on and off on and off on and off and so on.
how should i fix this problem?
files are here:
http://www.avisavis.no/game/as3ball.swf
http://www.avisavis.no/game/as3ball.as
http://www.avisavis.no/game/as3ball.fla
package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
public class as3ball extends MovieClip {
var the_hero: Sprite = new ball();
var the_ring: Sprite = new ring();
var power = 15.7;
var friction = 0.50;
var xspeed = 0;
var yspeed = 0;
var up = false;
var down = false;
var left = false;
var right = false;
var action1 = false;
public function as3ball() {
addChild(the_hero);
the_hero.x = 250;
the_hero.y = 200;
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, key_released);
addEventListener(Event.ENTER_FRAME, render);
}
function key_pressed(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP :
up = true;
break;
case Keyboard.DOWN :
down = true;
break;
case Keyboard.LEFT :
left = true;
break;
case Keyboard.RIGHT :
right = true;
break;
case Keyboard.CONTROL :
action1 = true;
break;
}
}
function key_released(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP :
up = false;
break;
case Keyboard.DOWN :
down = false;
break;
case Keyboard.LEFT :
left = false;
break;
case Keyboard.RIGHT :
right = false;
break;
case Keyboard.CONTROL :
action1 = false;
break;
}
}
private function render(e:Event):void {
if (up) {
if (the_hero.y >45){ yspeed -= power; ybox.text = String(the_hero.y); }
}
if (down) {
if (the_hero.y <980) { yspeed += power; ybox.text = String(the_hero.y); }
}
if (left) {
if (the_hero.x >45) { xspeed -= power; xbox.text = String(the_hero.x); }
}
if (right) {
if (the_hero.x <1230) { xspeed += power; xbox.text = String(the_hero.x); }
}
the_ring.x = the_hero.x + 15;
the_ring.y = the_hero.y + 35;
if (action1) {
if (this.contains(the_ring) ) { removeChild(the_ring); }
else { addChild(the_ring); }
}
xspeed *= friction;
yspeed *= friction;
the_hero.x += xspeed;
the_hero.y += yspeed;
}
}
}
the CTRL button in this game is doing two things:
if movieclip is visible, then turn it off.
if movieclip is not visible, then turn it on.
however, if i hold the CTRL button pressed, it will trigger over and over, turning the movieclip on and off on and off on and off and so on.
how should i fix this problem?
files are here:
http://www.avisavis.no/game/as3ball.swf
http://www.avisavis.no/game/as3ball.as
http://www.avisavis.no/game/as3ball.fla
package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
public class as3ball extends MovieClip {
var the_hero: Sprite = new ball();
var the_ring: Sprite = new ring();
var power = 15.7;
var friction = 0.50;
var xspeed = 0;
var yspeed = 0;
var up = false;
var down = false;
var left = false;
var right = false;
var action1 = false;
public function as3ball() {
addChild(the_hero);
the_hero.x = 250;
the_hero.y = 200;
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, key_released);
addEventListener(Event.ENTER_FRAME, render);
}
function key_pressed(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP :
up = true;
break;
case Keyboard.DOWN :
down = true;
break;
case Keyboard.LEFT :
left = true;
break;
case Keyboard.RIGHT :
right = true;
break;
case Keyboard.CONTROL :
action1 = true;
break;
}
}
function key_released(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP :
up = false;
break;
case Keyboard.DOWN :
down = false;
break;
case Keyboard.LEFT :
left = false;
break;
case Keyboard.RIGHT :
right = false;
break;
case Keyboard.CONTROL :
action1 = false;
break;
}
}
private function render(e:Event):void {
if (up) {
if (the_hero.y >45){ yspeed -= power; ybox.text = String(the_hero.y); }
}
if (down) {
if (the_hero.y <980) { yspeed += power; ybox.text = String(the_hero.y); }
}
if (left) {
if (the_hero.x >45) { xspeed -= power; xbox.text = String(the_hero.x); }
}
if (right) {
if (the_hero.x <1230) { xspeed += power; xbox.text = String(the_hero.x); }
}
the_ring.x = the_hero.x + 15;
the_ring.y = the_hero.y + 35;
if (action1) {
if (this.contains(the_ring) ) { removeChild(the_ring); }
else { addChild(the_ring); }
}
xspeed *= friction;
yspeed *= friction;
the_hero.x += xspeed;
the_hero.y += yspeed;
}
}
}