Copy link to clipboard
Copied
Ok, bare with me, as my English may not be so good. And I'm still not a professional in AS.
I made a dancing game. A game which objects will fly up to the targets and you have to press the right keys for them. I know this is kind of the question about multiple keypress thing. But I tried look up on them. I still can't figure it out. And...because I have nearly finished my project, but those how-to-make-multiple-keys answers , I may have to recreate everything, start from 0 again.
I have made a function to put the MCs on screen and run by a class.
Then, I have made a function which detect if the target hit the right position and the right keys is pressed.
Also if the wrong keys is pressed the score will decressed.
I didnt' made only one fuction detect the keys and than say what to do .
So my question is " Are there any other ways to make more than 1 kaycode instead of make a switch-case thing, for example?"
This is some part of my code.
var p1:Number=37;// 37Left(Left)
///
function makeLvl(e:Event):void
{
if (sTime < sTempo)
{
sTime++;
}
else
{
sTime = 0;
if (SelectedLvl[sArrow] != 0)
{
var currentArrow:MovieClip;//this will hold the current arrow
if (SelectedLvl[sArrow] == 1)
{
//place a left arrow onto the stage
currentArrow = new A1();
//set the _x value of the arrow so that it is in the
//right place to touch the receptor
currentArrow.x = 120;
//set the arrow's y coordinate off of the stage
//so that the user can't see it when it appears
currentArrow.y = 400;
currentArrow.arrowCode = p1 ;
addChild(currentArrow);//add it to stage
}
////
touchL = false; |
////
for (var i:int = 0; i<numChildren; i++)
{
var target:Object = getChildAt(i);
if (target.arrowCode != null && target.hitTestObject(mcReceptor))
{//if the object is an arrow and the receptor is touching it
if (target.arrowCode == p1)
{//if left arrow
touchL = true;
}
mcTxt.txtScore.text = 'Score: ' + score;
mcTxt.txtCombo.text = 'Combo: ' + combo;
mcTxt.txtScoreString.text = scoreString;
}
////
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeys);
function checkKeys(e:KeyboardEvent):void
{
//if the left key is down and no left arrows are touching the receptor
if (e.keyCode == p1 && ! touchL)
{
changeHealth(-10);//make the health go down
combo = 0;
scoreString = 'Bad';
}
beginCode();
////
I've try put an Array and chage it to Number to put fit it in, but no luck.
May be I used the Array wrong?
Any ideas? Any suggestions? -- Please....
Thanks you so so much .
This may be more advanced than what you're ready for, but this is one way to do it
http://blog.iconara.net/2008/03/30/separating-event-handling-from-event-filtering/
I've actually done this with a "chain of responsibility," where each link has a determineResponsibility callback and handleResponsibility callback. If it doesn't handle the responsibility (because it's not responsible), it sends the request on to the next link in the chain, which handles it or doesn't. If you're not familiar with a ca
...Copy link to clipboard
Copied
You can always use a series of if/else conditional tests to achieve a similar result to a swutch-case usage. Why are you objecting to using a switch-case?
Copy link to clipboard
Copied
Oh.. I didn't mean that I object to use the switch-case. I don't know how to apply it with my code since I have code it this way, unless I recode it all wich I don't want to. My idea is to find a way to put more than just one number in to p1 wich is at " currentArrow.arrowCode = p1;". So that everything may still run smooth like when I use only one key.
Whether the if/else or switch-case, can you guide me some more how can I work with these?
Copy link to clipboard
Copied
You can use conditionals instead of using switch-case coding... in cases where multiple conditions need to be checked you would end up needing to use conditionals anyways, so building up a conditional-based scheme can be a practical solution.
I suggest you search Google using terms like "AS3 multiple key code" to see how others have dealt with this scenario. The very first link in that search result is provided below and shows one way of keeping track of which keys are in use when processing multiple key conditionals. You will find it is followed by numerous other results that will prove to be helpful.
http://stackoverflow.com/questions/7975668/how-to-detect-multiple-key-down-event-in-as3
Copy link to clipboard
Copied
This may be more advanced than what you're ready for, but this is one way to do it
http://blog.iconara.net/2008/03/30/separating-event-handling-from-event-filtering/
I've actually done this with a "chain of responsibility," where each link has a determineResponsibility callback and handleResponsibility callback. If it doesn't handle the responsibility (because it's not responsible), it sends the request on to the next link in the chain, which handles it or doesn't. If you're not familiar with a callback, it's a variable of type Function so that you can change what function gets called at various times.
To clarify, you'd hand the first link in the chain a reference to the event, then call something like a process() method with a reference to the Event. This would call determineResponsibility(), which would look at the event to see if the characteristics of the event are what you want. If it returns true, it would call handleResponsibility(). If it returns false, you would call process() on the next link. The advantage of this design is that you can add and remove links as needed to handle changing needs in your system. For example, if the character hits something that disables right movement until the user hits the left arrow, remove the right arrow link until the user hits the left arrow, then put it back.
Message was edited by: Amy Blankenship
Find more inspiration, events, and resources on the new Adobe Community
Explore Now