LGCK event model
The engine uses handlers (such as the one below) to respond to various event. A handler is just a code snippet that is called when a condition is reached.
-- this a simple ai for a drone monster that moves LEFT/RIGHT. unMapSprite(self); if canFall ( self) then moveSprite( self, DOWN) ; playSound ("FALL"); else if ticks % 5 == 0 then local x; local y; local aim; x , y, aim = getSpriteVars(self) if isPlayerThere(self, aim) then attackPlayer ( self ); end if aim < LEFT or aim > RIGHT then aim = LEFT end if canMove(self, aim) then moveSprite(self, aim); if canFall(self) then aim = 5 - aim; moveSprite(self, aim); end else aim = XOR(aim, 1); end setAim ( self, aim ); end end mapSprite(self);
onInitGameThis event is fired the game starts.
This event is fired when a level starts.
This event is fired when a level is completed.
This event is fired when the player is killed.
This event is fired when all the goal objects are collected.
This event is fired when any goal object is killed (or pick-up which is the same concept in LGCK).
This event fires whenever a pick-up trigger is killed. The classic example is the diamond or flower. Both can be made pick-up trigger class .
This event fires when a save game is being restored.
This event fires when a save game is being created.
This event is triggered when the level is restarted. The usually fires up when the player is killed and has to start the level again.
This event fires up when the level is about to close. In between having completed the objective and completing the level and leaving the level.
onCreateThis method is called when the level is first created.
onDrawThis method is called when it's time to draw the screen.
onRestartThis method is called when the level is restarted.
onGoalCollectedThis method is called when all the goals are collected just before the level is completed
onGoalKilledThis method is called when a sprite marked as a goal is killed.
onLevelCompletedThis method is called just before moving to the next level.
onTimeOutThis method is called when timeLeft reaches zero.
onKeyPressedThis method is called when a key is presed. Use getLastKey() to retrieve the keyCode.
onKeyUpThis method is called when a key is released. Use getLastKey() to retrieve the keyCode.
This method is called on every tick for each level.
This works the way as same as the game notify closure event.
This method is called while the introduction screen is being displayed. You can override this to display custom text.
OnSpawnThis method is called when this object is created ( respawns included ).
onActivateThis method is called when this object is activated.
onDeathThis method is called when this object is destroyed (death, picked up etc.)
onHurtThis method is called when this object is hurt. Only applies to player at the moment.
onTouchThis method is called when the player touches this object.
onTriggerThis method is when a trigger is called on this object. Trigger key on this object must match that of the source for this event to be called.
onHandlerThis method is called when the object handler is called. You can use this method to redefine the custom monster class for example.
onSplatThis method is called when the player falls on this object.
onHitTestThis method is called when the player hit this object from below. (player aim supplied)
onZKeyThis method is called z-key action performed on this object
onAutoThis method is executed when automated timer rings
onLeftClickThis method is called when the player left click on this sprite
onRightClickThis method is called when the player right click on this sprite
onJumpThis method is called when the player jumps.
onFallThis method is called then object falls.
onLandingThis method is called when the object lands after a fall.
onLeapThis method is called when object performs as leap. Similar to onMove.
onMoveThis method is called when the object is moving (e.g. walking). This is currently only implemented for the player.
onFireThis method is called when the player/monster is firing.
onNotifyClosureThis works the way as same as the game notify closure event.
The game engine recognizes many objects. These objects are numbered by their ObjectType (or ObjType) and can be referenced to by the SPRITE_XXXX constants. A type refers to an object not an instance.
An object instance (also called a sprite) is simply an occurrence of the object found on a level. ObjId (or ObjectId) refers to a specific instance of an object. Each of these occurrences will have special properties such as a trigger key, visibility, frozen and goal attributes. In an object event handler, the objectId will always be available via the local variable `self`. (see examples)