PDA

View Full Version : [AS3] Help with a Rhythm like game


egmcmac
12-17-2008, 09:33 PM
Hey, I'm new here, but I was wondering if anyone could help me out. I've been trying to make a guitarhero like game for quite awhile. I finally found this tutorial on how to make a DDR, so I basically took that, changed a few models around and attempted to make it guitar hero based. My AS is pretty simple.

What I need to know is how to make it so that rather than pressing A, S, D, F for the button AND strumming, to make it so that you also need to press backspace. I have tried a variety of things, but hopefully you people will know how to fix it. My current AS is below. Everything else is working perfectly.
Also heres something to help you out;
Green button is called arrowLeft
Red button is called arrowUp
Yellow button is called arrowDown
Blue button is called arrowRight

ACTIONSCRIPT!!!
stop();
var sTime:Number = 0;
var sTempo:Number = 12;
var sArrow:Number = 0;
var arrowSpeed:Number = 4;
var gameIsOver:Boolean = false;
var score:Number = 0;
var scoreString:String = '';
var touchLeft:Boolean = false;
var touchUp:Boolean = false;
var touchDown:Boolean = false;
var touchRight:Boolean = false;
var combo:Number = 0;

function beginCode():Void {
onEnterFrame = function () {
makeLvl();
};
lvlArrayAll[lvlCurrent].push(0,0,0,0,0);
}
function makeLvl():Void {
if (sTime<sTempo) {
sTime++;
} else {
sTime = 0;
if (lvlArrayAll[lvlCurrent][sArrow] != 0) {
if (lvlArrayAll[lvlCurrent][sArrow] == 1) {
attachMovie('arrowLeft','arrow'+sArrow,getNextHigh estDepth());
_root['arrow'+sArrow]._x = 135;
_root['arrow'+sArrow].arrowCode = 65;
} else if (lvlArrayAll[lvlCurrent][sArrow] == 2) {
attachMovie('arrowUp','arrow'+sArrow,getNextHighes tDepth());
_root['arrow'+sArrow]._x = 205;
_root['arrow'+sArrow].arrowCode = 83;
} else if (lvlArrayAll[lvlCurrent][sArrow] == 3) {
attachMovie('arrowDown','arrow'+sArrow,getNextHigh estDepth());
_root['arrow'+sArrow]._x = 275;
_root['arrow'+sArrow].arrowCode = 68;
} else if (lvlArrayAll[lvlCurrent][sArrow] == 4) {
attachMovie('arrowRight','arrow'+sArrow,getNextHig hestDepth());
_root['arrow'+sArrow]._x = 345;
_root['arrow'+sArrow].arrowCode = 70;
}
_root['arrow'+sArrow]._y = 195;
_root['arrow'+sArrow].gotHit = false;
_root['arrow'+sArrow].onEnterFrame = function() {
this._y += arrowSpeed;
if (this.hitTest(_root.mcReceptor) && !this.gotHit) {
if (Key.isDown(this.arrowCode)) {
if (this._y<=mcReceptor._y+15 && this._y>=mcReceptor._y+5) {
score += 10;
scoreString = 'Perfect';
changeHealth(5);
} else if (this._y<=mcReceptor._y+25 && this._y>=mcReceptor._y-5) {
score += 8;
scoreString = 'Great';
changeHealth(4);
} else if (this._y<=mcReceptor._y+40 && this._y>=mcReceptor._y-30) {
score += 6;
scoreString = 'Nice';
changeHealth(3);
} else {
score += 4;
scoreString = 'Good';
changeHealth(2);
}
combo++;
this._visible = false;
this.gotHit = true;
}
}
if (this._y>=400) {
if (!this.gotHit) {
changeHealth(-10);
scoreString = 'Bad';
combo = 0;
}
this.removeMovieClip();
}
};
}
if (sArrow<lvlArrayAll[lvlCurrent].length && !gameIsOver) {
sArrow++;
} else {
gotoAndStop('win');
gameIsOver = true;
for (var i:Number = 0; i<sArrow; i++) {
_root['arrow'+i].removeMovieClip();
}
onEnterFrame = null;
}
}
}
function changeHealth(healthDiff:Number):Void {
if (mcHealth._xscale+healthDiff>=100) {
mcHealth._xscale = 100;
} else if (mcHealth._xscale+healthDiff<=0) {
gameIsOver = true;
for (var i:Number = 0; i<sArrow; i++) {
_root['arrow'+i].removeMovieClip();
}
gotoAndStop('lose');
onEnterFrame = null;
} else {
mcHealth._xscale += healthDiff;
}
}
mcReceptor.onEnterFrame = function() {
touchLeft = false;
touchUp = false;
touchDown = false;
touchRight = false;
for (i=sArrow-10; i<=sArrow; i++) {
if (this.hitTest(_root['arrow'+i]) && _root['arrow'+i].arrowCode == 65) {
touchLeft = true;
}

if (this.hitTest(_root['arrow'+i]) && _root['arrow'+i].arrowCode == Key.BACKSPACE) {
touchUp = true;

}

if (this.hitTest(_root['arrow'+i]) && _root['arrow'+i].arrowCode == Key.BACKSPACE) {
touchDown = true;
}

if (this.hitTest(_root['arrow'+i]) && _root['arrow'+i].arrowCode == Key.BACKSPACE) {
touchRight = true;
}


}
if (Key.isDown(Key.BACKSPACE) && !touchLeft) {
changeHealth(-10);
scoreString = 'Bad';
combo = 0;
}
};

beginCode();

Thanks again,
EgMcMac :o

NickZA
12-18-2008, 11:13 PM
Hey,

You need to have an array in which you store the state of all keys relevant to your game; state meaning whether the key is pressed or unpressed during a given game tick/frame.

So for example if my game only used the up and down arrow keys, I would have an array like this:

var array = new Array(); //will probably be in object scope, not function scope
array[38] = false; //38 is the keycode for up arrow
array[40] = false; //40 is the keycode for up arrow

You then need to set up an event listener to get and use these keyboard events:
KeyboardEvent.KEY_DOWN
KeyboardEvent.KEY_UP
Your event handler function for KEY_DOWN will set the array entry to true, like so:
function keyDownHandler(e:KeyboardEvent):void
{
trace("key "+keyCode+" just went down!");
_keysPressed[__keyCode] = true;
}
And for your KEY_UP handler, do something like:
function keyUpHandler(e:KeyboardEvent):void
{
trace("key "+keyCode+" just went up!");
_keysPressed[__keyCode] = false; //note, it's now false.
}

You can see that the whole array is not cleared of keypresses every update -- it is cleared only for a single key and only when they KEY_UP event is actually fired by Flash itself.

Now you can simply check...

if (_keysPressed[38] == true && _keysPressed[38] == true) //if up arrow AND down arrow are pressed on this game tick
{
//do whatever the hell it is you wanna do :)
}

For more info, look at Jeff Fulton's brilliant site (http://www.8bitrocket.com/newsdisplay.aspx?newspage=6249).