Class: Character

Character()

new Character()

Represents the main character in the game, extending the MovableObject class. Manages animations, movement, collisions, and idle states (including a long idle with snoring).
Source:

Extends

Members

IMAGES_DEAD :Array.<string>

An array of image paths for the dead (game over) animation.
Type:
  • Array.<string>
Source:

IMAGES_HURT :Array.<string>

An array of image paths for the hurt animation.
Type:
  • Array.<string>
Source:

IMAGES_IDLE :Array.<string>

An array of image paths for the default idle animation.
Type:
  • Array.<string>
Source:

IMAGES_JUMPING :Array.<string>

An array of image paths for the jumping animation.
Type:
  • Array.<string>
Source:

IMAGES_LONG_IDLE :Array.<string>

An array of image paths for the long idle animation (snoring).
Type:
  • Array.<string>
Source:

IMAGES_WALKING :Array.<string>

An array of image paths for the walking animation.
Type:
  • Array.<string>
Source:

acceleration :number

Acceleration used for gravity effects.
Type:
  • number
Inherited From:
Source:

characterInterval :number

Reference to the interval handling character animations.
Type:
  • number
Source:

currentImage :number

The current image index for animations.
Type:
  • number
Overrides:
Source:

enemyIsDead :boolean

Indicates if the enemy is dead. Not used for characters.
Type:
  • boolean
Overrides:
Source:

energy :number

The energy level of the object.
Type:
  • number
Overrides:
Source:

height :number

The height of the character.
Type:
  • number
Source:

lastHit :number

Timestamp of the last time the object was hit.
Type:
  • number
Inherited From:
Source:

longIdle :boolean

Indicates whether the character is in a long idle state.
Type:
  • boolean
Source:

moveInterval :number|undefined

Interval ID for movement-related loops.
Type:
  • number | undefined
Inherited From:
Source:

moveIntervall :number

Reference to the interval handling character movement.
Type:
  • number
Source:

otherDirektion :boolean

Indicates if the character is facing the opposite direction (left).
Type:
  • boolean
Source:

speed :number

The movement speed of the character.
Type:
  • number
Overrides:
Source:

speedY :number

Vertical speed of the object.
Type:
  • number
Inherited From:
Source:

timeoutLongIdle :number|null

Timeout reference for triggering the long idle state.
Type:
  • number | null
Source:

world :object

Reference to the game world object.
Type:
  • object
Source:

x :number

The x-coordinate of the character.
Type:
  • number
Source:

y :number

The y-coordinate of the character.
Type:
  • number
Source:

Methods

animate()

Starts the movement and animation intervals, updating the character's position and state.
Source:

applyGravity()

Applies gravity to the object by updating its vertical position. The object's vertical speed is decreased by its acceleration at regular intervals.
Inherited From:
Source:

handleAnimation()

Controls the character's animation state on each frame based on conditions like death, hurt, jumping, or idle.
Source:

handleCollision(oldX)

Checks for collisions with enemies and reverts the character's x-position if a collision is detected.
Parameters:
Name Type Description
oldX number The character's x-position before movement.
Source:

handleDeadAnimation(frameIndex)

Plays the dead animation frame-by-frame, then stops character updates once the sequence is complete.
Parameters:
Name Type Description
frameIndex number The current frame index in the dead animation sequence.
Source:

handleDirection()

Handles horizontal movement based on keyboard input.
Source:

handleHurtAnimation()

Plays the hurt animation frames, stops the long idle timer, and stops the snoring sound.
Source:

handleIdleAnimation()

Determines whether to play the walking animation, long idle (snoring) animation, or default idle animation based on keyboard input and the long idle timer.
Source:

handleJump()

Allows the character to jump if the UP or SPACE key is pressed and the character is on the ground.
Source:

handleJumpingAnimation()

Plays the jumping animation frames, stops the long idle timer, and stops the snoring sound.
Source:

handleMovement()

Handles the character's movement on each frame, detecting directional input and collisions.
Source:

hit()

Reduces the object's energy when hit. If the energy drops below zero, it is set to zero. Otherwise, the last hit timestamp is updated.
Inherited From:
Source:

isAboveGround() → {boolean}

Determines if the object is currently above the ground. Ground detection differs based on the type of object.
Inherited From:
Source:
Returns:
True if the object is above the ground, otherwise false.
Type
boolean

isColliding(obj) → {boolean}

Checks if this object is colliding with another movable object. Collision detection accounts for offset values for more accurate boundaries.
Parameters:
Name Type Description
obj MovableObject The other object to check collision against.
Inherited From:
Source:
Returns:
True if a collision is detected, otherwise false.
Type
boolean

isDead() → {boolean}

Determines if the object is dead based on its energy level.
Inherited From:
Source:
Returns:
True if the object's energy is zero, otherwise false.
Type
boolean

isHurt() → {boolean}

Checks if the object is currently in a hurt state. An object is considered hurt if it was hit within the last second.
Inherited From:
Source:
Returns:
True if the object is hurt, otherwise false.
Type
boolean

jump()

Initiates a jump by setting the vertical speed to a positive value.
Inherited From:
Source:

moveLeft()

Moves the object to the left by its horizontal speed.
Inherited From:
Source:

moveRight()

Moves the object to the right by its horizontal speed.
Inherited From:
Source:

playAnimation(images)

Animates the object by cycling through a set of images. The displayed image is updated based on the current animation frame.
Parameters:
Name Type Description
images Array.<string> An array of image paths used for the animation.
Inherited From:
Source:

resetCharacter()

Resets the character to its default state for a new game or after certain conditions, restoring position, energy, and other state values.
Source:

startTimer()

Starts the timer that triggers the long idle animation if there is no interaction for 15 seconds.
Source:

stopTimer()

Stops the long idle timer and resets its state.
Source:

Character()

new Character()

Constructs the Character, loads images, applies gravity, and starts the animation loops.
Source:

Members

IMAGES_DEAD :Array.<string>

An array of image paths for the dead (game over) animation.
Type:
  • Array.<string>
Source:

IMAGES_HURT :Array.<string>

An array of image paths for the hurt animation.
Type:
  • Array.<string>
Source:

IMAGES_IDLE :Array.<string>

An array of image paths for the default idle animation.
Type:
  • Array.<string>
Source:

IMAGES_JUMPING :Array.<string>

An array of image paths for the jumping animation.
Type:
  • Array.<string>
Source:

IMAGES_LONG_IDLE :Array.<string>

An array of image paths for the long idle animation (snoring).
Type:
  • Array.<string>
Source:

IMAGES_WALKING :Array.<string>

An array of image paths for the walking animation.
Type:
  • Array.<string>
Source:

acceleration :number

Acceleration used for gravity effects.
Type:
  • number
Inherited From:
Source:

characterInterval :number

Reference to the interval handling character animations.
Type:
  • number
Source:

currentImage :number

The current image index for animations.
Type:
  • number
Overrides:
Source:

enemyIsDead :boolean

Indicates if the enemy is dead. Not used for characters.
Type:
  • boolean
Overrides:
Source:

energy :number

The energy level of the object.
Type:
  • number
Overrides:
Source:

height :number

The height of the character.
Type:
  • number
Source:

lastHit :number

Timestamp of the last time the object was hit.
Type:
  • number
Inherited From:
Source:

longIdle :boolean

Indicates whether the character is in a long idle state.
Type:
  • boolean
Source:

moveInterval :number|undefined

Interval ID for movement-related loops.
Type:
  • number | undefined
Inherited From:
Source:

moveIntervall :number

Reference to the interval handling character movement.
Type:
  • number
Source:

otherDirektion :boolean

Indicates if the character is facing the opposite direction (left).
Type:
  • boolean
Source:

speed :number

The movement speed of the character.
Type:
  • number
Overrides:
Source:

speedY :number

Vertical speed of the object.
Type:
  • number
Inherited From:
Source:

timeoutLongIdle :number|null

Timeout reference for triggering the long idle state.
Type:
  • number | null
Source:

world :object

Reference to the game world object.
Type:
  • object
Source:

x :number

The x-coordinate of the character.
Type:
  • number
Source:

y :number

The y-coordinate of the character.
Type:
  • number
Source:

Methods

animate()

Starts the movement and animation intervals, updating the character's position and state.
Source:

applyGravity()

Applies gravity to the object by updating its vertical position. The object's vertical speed is decreased by its acceleration at regular intervals.
Inherited From:
Source:

handleAnimation()

Controls the character's animation state on each frame based on conditions like death, hurt, jumping, or idle.
Source:

handleCollision(oldX)

Checks for collisions with enemies and reverts the character's x-position if a collision is detected.
Parameters:
Name Type Description
oldX number The character's x-position before movement.
Source:

handleDeadAnimation(frameIndex)

Plays the dead animation frame-by-frame, then stops character updates once the sequence is complete.
Parameters:
Name Type Description
frameIndex number The current frame index in the dead animation sequence.
Source:

handleDirection()

Handles horizontal movement based on keyboard input.
Source:

handleHurtAnimation()

Plays the hurt animation frames, stops the long idle timer, and stops the snoring sound.
Source:

handleIdleAnimation()

Determines whether to play the walking animation, long idle (snoring) animation, or default idle animation based on keyboard input and the long idle timer.
Source:

handleJump()

Allows the character to jump if the UP or SPACE key is pressed and the character is on the ground.
Source:

handleJumpingAnimation()

Plays the jumping animation frames, stops the long idle timer, and stops the snoring sound.
Source:

handleMovement()

Handles the character's movement on each frame, detecting directional input and collisions.
Source:

hit()

Reduces the object's energy when hit. If the energy drops below zero, it is set to zero. Otherwise, the last hit timestamp is updated.
Inherited From:
Source:

isAboveGround() → {boolean}

Determines if the object is currently above the ground. Ground detection differs based on the type of object.
Inherited From:
Source:
Returns:
True if the object is above the ground, otherwise false.
Type
boolean

isColliding(obj) → {boolean}

Checks if this object is colliding with another movable object. Collision detection accounts for offset values for more accurate boundaries.
Parameters:
Name Type Description
obj MovableObject The other object to check collision against.
Inherited From:
Source:
Returns:
True if a collision is detected, otherwise false.
Type
boolean

isDead() → {boolean}

Determines if the object is dead based on its energy level.
Inherited From:
Source:
Returns:
True if the object's energy is zero, otherwise false.
Type
boolean

isHurt() → {boolean}

Checks if the object is currently in a hurt state. An object is considered hurt if it was hit within the last second.
Inherited From:
Source:
Returns:
True if the object is hurt, otherwise false.
Type
boolean

jump()

Initiates a jump by setting the vertical speed to a positive value.
Inherited From:
Source:

moveLeft()

Moves the object to the left by its horizontal speed.
Inherited From:
Source:

moveRight()

Moves the object to the right by its horizontal speed.
Inherited From:
Source:

playAnimation(images)

Animates the object by cycling through a set of images. The displayed image is updated based on the current animation frame.
Parameters:
Name Type Description
images Array.<string> An array of image paths used for the animation.
Inherited From:
Source:

resetCharacter()

Resets the character to its default state for a new game or after certain conditions, restoring position, energy, and other state values.
Source:

startTimer()

Starts the timer that triggers the long idle animation if there is no interaction for 15 seconds.
Source:

stopTimer()

Stops the long idle timer and resets its state.
Source: