Class: MovableObject

MovableObject()

new MovableObject()

Represents a movable object in the game, extending the DrawableObject class. Handles physics (gravity), collisions, movement, and animations for both the character and enemies.
Source:

Extends

Members

acceleration :number

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

currentImage :number

Index of the current image in the animation cycle.
Type:
  • number
Source:

enemyIsDead :boolean

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

energy :number

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

height :number

The height of the drawable object.
Type:
  • number
Inherited From:
Source:

imageCache :Object.<string, HTMLImageElement>

A cache for storing images, with the image path as the key.
Type:
  • Object.<string, HTMLImageElement>
Inherited From:
Source:

img :HTMLImageElement

The image used for the drawable object.
Type:
  • HTMLImageElement
Overrides:
Source:

lastHit :number

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

moveInterval :number|undefined

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

offset :Object

Offset values for adjusting collision detection or rendering.
Type:
  • Object
Inherited From:
Source:

speed :number

Horizontal speed of the object.
Type:
  • number
Source:

speedY :number

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

width :number

The width of the drawable object.
Type:
  • number
Inherited From:
Source:

x :number

The x-coordinate position of the drawable object.
Type:
  • number
Overrides:
Source:

y :number

The y-coordinate position of the drawable object.
Type:
  • number
Overrides:
Source:

Methods

applyGravity()

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

draw(ctx)

Draws the current image on the provided canvas context.
Parameters:
Name Type Description
ctx CanvasRenderingContext2D The drawing context of a canvas.
Inherited From:
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.
Source:

isAboveGround() → {boolean}

Determines if the object is currently above the ground. Ground detection differs based on the type of object.
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.
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.
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.
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.
Source:

loadImage(path)

Loads a single image from the specified path.
Parameters:
Name Type Description
path string The path of the image to load.
Inherited From:
Source:

loadImages(arr)

Loads multiple images and stores them in the image cache.
Parameters:
Name Type Description
arr Array.<string> An array of image paths.
Inherited From:
Source:

moveLeft()

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

moveRight()

Moves the object to the right by its horizontal speed.
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.
Source: