ActionScript 3.0 Game Programming
KEY_UP, KEY_DOWN Woes
Submitted by aetharr on Sun, 03/07/2010 - 16:56.Ok, here is my problem. Using the system you illustrated in your book for keyboard movement, I have a problem which I just cannot seem to figure out. Here is the code for movement, which you'll have seen a million times already :P
public var leftArrow,rightArrow,downArrow,upArrow:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
public function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37 || event.keyCode == 65) {
leftArrow = true;
} else if (event.keyCode == 39 || event.keyCode == 68) {
rightArrow = true;
} else if (event.keyCode == 38 || event.keyCode == 87) {
upArrow = true;
} else if (event.keyCode == 40 || event.keyCode == 83) {
downArrow = true;
}
}
public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 37 || event.keyCode == 65) {
leftArrow = false;
} else if (event.keyCode == 39 || event.keyCode == 68) {
rightArrow = false;
} else if (event.keyCode == 38 || event.keyCode == 87) {
upArrow = false;
} else if (event.keyCode == 40 || event.keyCode == 83) {
downArrow = false;
}
}
Ok, so that's the code I use, everything else, acts on if the boolean variables are set to true or false, like in your example. The only modification to the code is the extra keyCode which enables the user to use UP,DOWN,LEFT,RIGHT And or, WASD too. However, that is not final, and the finished product will likely have the ability to change these keys... Anyway.
My problem is this:
The system is awesome, unless.. If WHILE HOLDING a key, the user right-clicks, or sometimes it happens on it's own, perhaps if the keyboard thinks you're pressing too many keys. ( It would usually happen too if the window lost focus, but I have a function in place to automatically pause the game and set all the movement variables to false in this case, so it does not matter. ) The game doesn't register that the user has released a key, and because of this, the variable remains true, while the user ISNT holding the key down. The only way to counter this is to have the window lose focus and it resets all of the variables to false, Or to have the user press the button corresponding to the True variable, and then release it.
Is there a way I can prevent this from happening? I would really appreciate your assistance.
Thanks for taking to time to read,
Regards
Pete
XML level editor
Submitted by jdsantiagojr on Fri, 03/05/2010 - 17:30.I am trying to find some information on making levels in my game using XML. Can any one point me in the right direction. I would like to create 10 levels but with out all the class files. is there away to use mainclass.as and load stage one stage two etc.?
I have separate class files for my characters and enemies but would like to use xml, for stages. How do I?
help me please ..to make rapid press
Submitted by kiroszz on Wed, 03/03/2010 - 14:52.I have a problem making character attack with AS3 ,.. when I try programing one press keyboard (key space) to attack, it will happen continoussly when the key holding down press... I just need one tab press then the attack excute,.. not holding press .. help me please.... I beg U all,.. btw sorry for bad english ... here my source code:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Main extends MovieClip
{
var vx:int;
var vy:int;
var serang:Boolean=true;
public function Main()
{
init();
}
function init():void
{
vx=0;
vy=0;
serang=false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyTekan);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyLepas);
stage.addEventListener(Event.ENTER_FRAME, onEnterframe);
}
function onKeyTekan(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
if (serang == true)
{
Player.gotoAndStop(3);
serang == false;
}
}
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
Player.gotoAndStop(2);
Player.scaleX = -0.3;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
Player.gotoAndStop(2);
Player.scaleX = 0.3;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
Player.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
Player.gotoAndStop(2);
}
}
function onKeyLepas(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
{
vx = 0;
Player.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
vy = 0;
Player.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.SPACE)
{
Player.gotoAndStop(1);
serang=true;
}
}
function onEnterframe(event:Event):void
{
Player.x+=vx;
Player.y+=vy;
}
}
}
1137
Submitted by ingcha on Tue, 03/02/2010 - 14:54.I got error on this, can anyone tell me what's wrong? n how to fix it? thanks.
1137: Incorrect number of arguments. Expected no more than 1.
{public function moveMeats(timeDiff:int)
{
for(var j:int=0;j
Found great article on Adobe.com
Submitted by mightybotme on Mon, 03/01/2010 - 17:19.I found an article that talks about an introduction to developing games on the Adobe Flash Platform. It really helped me out:
http://www.adobe.com/newsletters/edge/october2009/articles/article1/inde...
Issues with scrolling
Submitted by windwarp on Sat, 02/27/2010 - 00:16.Hello, I'm looking for some assistance with some side scrolling issues I'm having with my code. This is my first time really coding so any and all help/advice would be a big help! In total I have 3 timelines running, one for pure Action script, one for my movie clips, and a final for my background(this line I have not started to work with, and am not currently concerned about).
So here is my code(its a bit messy, please forgive me):
~~~~~~~
hero.gotoAndStop('still');
var friction:Number=.9;
var speed:Number=0;
var dy:Number=0;
var gravity:Number=4;
var canjump:Boolean = false;
var Key:KeyObject = new KeyObject(stage);
stage.addEventListener(Event.ENTER_FRAME,onenter);
scrollStage();
function scrollStage():void
{
ground.x += (stage.stageWidth * .5) + hero.x;
hero.x =(stage.stageWidth * 0.5);
}
function onenter(e:Event):void{
dy+=gravity;
if(hero.y>300){
canjump=true;
}
if(Key.isDown(Key.UP) && canjump){
dy=-25;
canjump=false;
}
hero.y+=dy;
if (Key.isDown(Key.RIGHT)){
speed-=6
hero.x+=12;
hero.scaleX=1;
hero.gotoAndStop('walking');
}else if (Key.isDown(Key.LEFT)){
speed+=6
hero.x+=-12;
hero.scaleX=-1;
hero.gotoAndStop('walking');
}else {
hero.gotoAndStop('still');
}
if (speed>20){
speed=20;
}
if (speed<(-20)) {
speed=-20;
}
speed=speed*friction;
hero.x-=speed;
if (Math.abs(speed)<0.0009);{
if (! ground.hitTestPoint(hero.x,hero.y,true)) {
hero.y+=dy;
canjump=false;
}
if (dy>10) {
dy=10;
}
for (var i:int = 0; i<10; i++) {
if (ground.hitTestPoint(hero.x,hero.y,true)) {
hero.y--;
dy=0;
canjump=true;
}
}
function scrollStage():void
{
ground.x += (stage.stageWidth * 1) - hero.x;
hero.x = stage.stageWidth * 1;
}
}
}
~~~~~~~~~~~~~~~~
Again any advice on my situation would be greatly appreciated.
Thanks
-Windwarp
Match Three - level Increase, bonus Points
Submitted by rayhtown on Thu, 02/25/2010 - 20:33.Any suggestions for adding levels increase when a score is met and bonus points on the next level on on game pieces.
Jigsaw Game: How to create realistic puzzle pieces?
Submitted by rickramble on Mon, 02/22/2010 - 18:32.Gary's book adds a tantalizing note on the subject of making "puzzle pieces look like traditional puzzle pieces with interlocking tabs. This cosmetic feature can be done in Flash, but only by using quite a bit of vector drawing and bitmap-manipulation tools...".
Having mastered the fundamentals, I'm interested in doing just that. Are there any resources available to show the way?
Tic Tac Toe?
Submitted by doreen on Sun, 02/21/2010 - 22:13.Any code for a really simple Tic Tac Toe game would be greatly appreciated. Thnx
Highscores
Submitted by rayhtown on Sat, 02/20/2010 - 21:40.I'm currently modifying the match three game any suggestions on quickly adding high score board feature.
Copyright Gary Rosenzweig
