Constructor
new PipeManager(scene, pipeCount, horizontalOffset, verticalOffset, startingX, velocityX)
Sets up all default values for all class variables
Parameters:
| Name | Type | Description |
|---|---|---|
scene |
the Phaser.Scene object used for adding images/sprites/physics/etc... | |
pipeCount |
the amount of pipes that gets created | |
horizontalOffset |
the distance between sets of pipes on the horizontal axis | |
verticalOffset |
the distance between the top and bottom pipe inside a PipeGroup on the vertical axis | |
startingX |
where on the canvas on the x-axis the pipes start from | |
velocityX |
how fast the collection of pipes move using Phaser's physics |
- Source:
Methods
create()
This method is called once and is used to create each PipeGroup gameObject
and sets up each pipe's position
- Source:
getNewPipePosition(index) → {Object}
Originally inside the setupPipePositions(pipe, index) method, however, this could be used
in other places. This method will get the position of the last element in the array if the
index isn't 0. It will use the previous pipe's position plus the horizontal offset as a new x position.
If it is the first index, being 0, then it will just set the position to whatever
the class starting position variable holds. Then it gets a new y position and returns both the x and y
variables inside an object.
Parameters:
| Name | Type | Description |
|---|---|---|
index |
the index of the current pipe to reposition |
- Source:
Returns:
- new x and y positions to use for a pipe
- Type
- Object
getRandomY() → {number}
This function generates a random number on the y-axis by offsetting the middle of the y-axis for
min and max numbers that the random generator uses
- Source:
Returns:
- new y position randomly generated to position a pipe on the vertical axis
- Type
- number
handlePipePool()
This method checks each of the pipe's position inside the pipes array,
and if the pipe is outside of the canvas, it will reset the pipe back to the
other end of all the other pipes.
- Source:
handleResetPipe()
This method will take in a PipeGroup object and since this pipe is beyond the canvas,
then we know that it is the first PipeGroup object inside the array. Therefore it will
remove the first element from the PipeGroup array, and appends it to the end. It also
positions the pipe to a new position relative to the previous last element and the
horizontal offset
- Source:
preload()
This function is called before anything is drawn on the canvas.
This allows for asset preloading which prevents anything from drawing without
it first loading the asset.
- Source:
resetG()
This method is used for resetting the pipe's positions and anything else that may have
been altered during the gameplay
- Source:
setHorizontalOffset(newOffset)
This method will change and update the distance between each pipe with the
given newOffset value. This can be used to update the offset mid-game which could
add more complexity to the game
- very cpu intensive and should not be used inside an update method
Parameters:
| Name | Type | Description |
|---|---|---|
newOffset |
the space between each pipe |
- Deprecated:
- Yes
- Source:
setVelocityX(velocity)
Setter for the velocityX variable
Parameters:
| Name | Type | Description |
|---|---|---|
velocity |
how fast the pipes are moving |
- Source:
setupPipePositions(pipe, index)
This method dynamically generates positions for each pipe while
taking in account for the horizontal offset given from the constructor
Parameters:
| Name | Type | Description |
|---|---|---|
pipe |
the pipe that is being positioned | |
index |
the index of that pipe from an array of pipes |
- Source:
startG()
Starts moving the pipes and updates the currentState to RUNNING
allowing for pipe pooling and one of the main components to start the gameplay
- Source:
stopG()
Stops the pipes at their current positions and disables their horizontal velocity.
Also changes the currentState to IDLE
- Source:
update()
The update method is called every frame and if the currentState variable = RUNNING, it calls the
handlePipePool() method to check if pipe needs to be repositioned
and updating the position if needed.
- Source: