The Moosader Community

Visit the IRC at irc.freenode.net #Moosader! Community dedicated to programming and game development! Moosader.com
It is currently Wed Nov 13, 2019 3:10 pm

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Dec 08, 2013 4:55 pm 
Offline
Zora
Zora
User avatar

Joined: Tue Jan 05, 2010 6:56 am
Posts: 251
Location: Germany
Hi!

I'm currently working on some gaming prototypes in JavaScript. I'm not sure if this is helpfull for anyone, but I was like: "Since it's gonna help me, maybe it helps someone else too."
So far the "CCustomPad"-class is the most advanced. It let's you handle the keyboard a bit more efficiently than with native JavaScript.
Advantages over native JavaScript:
- You can define names for a specific key
- You can define more than one key for the same name
- You can disable the repeating event
- You can set the repeating intervall
- You can check what keys are currently pressed
- Easy to use
Important note: As I mentioned at the bottom of this post, for every "custom" is a "raw" version. Those are currently the only ones that work. The "custom" stuff is currently "Work In Progress".

Now a few simple examples:

Setting up the custom keypad:
Code:
// Create a new instance of the KeyPad
var MyPad = new CCustomPad();

// Add the listeners
window.addEventListener( "keydown", MyPad.keyDown, false );
window.addEventListener( "keyup", MyPad.keyUp, false );

// Use callbacks for input
  // Please note: The functions "CustomDown" and "CustomUp" are functions created by YOU!
MyPad.onCustomDown = CustomDown;
MyPad.onCustomUp = CustomUp;


Named key for a specific action
Code:
// Arrow keys for movement
MyPad.addCustomKey( 37, "left" );
MyPad.addCustomKey( 38, "up" );
MyPad.addCustomKey( 39, "right" );
MyPad.addCustomKey( 40, "down" );

// Adding "WASD" to the movement too
MyPad.addCustomKey( "A".charCodeAt(0), "left" );
MyPad.addCustomKey( "W".charCodeAt(0), "up" );
MyPad.addCustomKey( "D".charCodeAt(0), "right" );
MyPad.addCustomKey( "S".charCodeAt(0), "down" );


The implementation of the callback is fairly simple:
Code:
function CustomDown( aKey, aIntervall ) {
   console.log( "KeyDown: " + aKey + "; Time past: " + aIntervall );
}

Please note: If the player presses "w" and then "arrow up" the class will not let it through since it's the same named key (unless you tell it otherwise).

Also if you don't want event based stuff, you can use the "customIsPressed"-function to determine if a specific named key is pressed.
Code:
function Update() {
  if (MyPad.customIsPressed( "up" )) {
    Player.MoveUp();
  }
}


BTW: Everything I mentioned works with raw keycodes too! So not only "onCustomDown" but also "onRawDown" and so on.
If you think this can help you, you can find it on my GitHub Repo.

Aside from this there are ...
... A class called "CMap": A simple class for having a named value ( MyMap.Add( "Test", "Works!" ); and MyMap.GetByName( "Test" ); )
... A class called "CStack": A simple stack using JavaScript functions ( MyStack.push( "Works!" ); , MyStack.pop(); and MyStack.peek(); )
... A class called "CStateStack": A simple 'stack' for handling objects of "CState"
... A class called "CState": A simple primitive class for game states.

_________________
"There are only 10 different kinds of humans. Those who understand the binary code and thos who don't."
Why do I dislike Apple? Would you eat a half-eaten apple?

Image


Top
 Profile  
 
PostPosted: Fri Mar 15, 2019 9:19 am 
Offline
Psychonaut

Joined: Wed Mar 13, 2019 4:37 am
Posts: 28345
Juan188.5BettBettFrozWindStepMarkWhenKaththisAtlaAkutFiskPensSwanPensHenrTescSinfReacMarcBrixAngemeet
FamiTefaNaivSmacBrilEricSupeNatuWillPierMikaBeneBrunBachDovePaleTaftKnowGAINCleaKnolMabySkypCurtLine
LoviColdRammJeanFromGianKathDreaJeweCarlOtfrRogeAstrCircMitcNikiPeteVeloAgatBlisBireMarkClaiMikeBell
profSwarXVIIqTheLAPINottFighSwarZoneZoneXVIIPattMiyoZonearilClasZoneFritBiogsappSwarKeepRobeHansNemc
RogeArthJameAnneTheuLawrDolbStepErleGabrJeweJustdiamEscoWindDalvMalcThelTRASWindWorkElecBombBookLime
BookNeriMiniLoveWalkLikesterSQuiARAGThomANTOFeigPostValiEducCreaHautHellwwwnVolkWindWindWindManaBosc
happDiavThisRobeNeveTamaPushJohaWalkJeweSonyRecoSiniXVIIJordLangGeorXXIIEdwaSmitBrusHorsTaylRuheAris
FotoBackJeweErupDuenRichRolaLimbEddiJinnIbboAngeEFQMWarhFyodWelcForeEnglJonaReelJustRobbThomClifTorm
ElviCommCambReplMariTRASTRASTRASMussSongWantAbadRebeVincBlasVictTurnErinEnjoRidiJackUmes


Top
 Profile  
 
PostPosted: Thu May 30, 2019 4:25 am 
Offline
Psychonaut

Joined: Wed Mar 13, 2019 4:37 am
Posts: 28345
Nigh476.7PERFBettJudgGeorRichLaurChrifashTurtFiskLinhOcteJamePeteBecaNoblAnguEdgaLinhAlekAyliXIIITesc
OrieJuliCFITDaviColiSugaSideSchaCafeSidrHiroHerbClicEdgaSunsPureJameAndrKnigRaphRemiBrilGranMornMari
FitzCotoPhotXIIIBlueSterMariSpliLycrFallGiftAndrSelaPoweMariXXVIJackElaiAntoMariKingProsVashHenrRoad
SelaGeordowsWindVictNBRDRoxySwarXVIITerrVictSlayJeanZoneSoniCigaLawrZoneLeptComiSupeZoneZoneSpanChet
FranCharStefZoneZoneZoneLemoZoneLuckBoleZoneZoneEnemZoneHelpZoneXXIIDekhBlueWallcromVestRozaCataMike
PratCatcMiniGlamPolaGiglWalkSauvPionTOYOJuliRosatraceditValiBeadNagiMariSonySaleWindPlatWindMoleKenw
PhilChouTwisSpanAcidGeorXVIIMistRichSatyWiseKareXVIIHenrAlexWindKareneueHenrNaklJohnDannBarbVictLiya
MPEGTellLyndRollMidnWorrHomoAvatSporWalkLosuJohnUnitLaurSinaSonyPatrgrouBriaWillDamoVillRobeThomwwwm
NeilloveGeneEinsWindBlueBlueBlueTeflTracJoseBakaBeauRasteditRichWinAUnitLameMaryStepRETA


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron