Hi, I am following a tutorial on YouTube, to make Infinity runner.
I am having trouble (no errors are showing when launch) but my Start Button (Play Btn) is not starting game when clicked and my player is only allowed to jump once.
Can someone check my scripts?
Beginner here.
Link to Editor: https://playcanvas.com/editor/scene/2149703
Hi @Ashley_Solano!
I took a look at your project and noticed a couple of things.
I see that you are firing the app level event 'GameManager:StartGame'
from the button and then listening for it in the GameManager
script. That works fine.
You then fire 'Player:StartGame'
and 'PlatformMovement:StartGame'
. Your Player
script hears this event and attempts to set the the animTrigger ‘start’ and set the state to run.
In my console, I see a notification that there are a couple of missing parameters that the app can’t find:
As for the reason that you can only jump once, you check to make sure that the character is not in the jump state before activiting it and seting the state to jump, but I don’t see anywhere else in your script where the state is then changed back away from jump:
Player.prototype.jump = function () {
const self = this;
if (this.state != 'jump') {
this.state = 'jump';
this.falling = true;
this.entity.anim.setBoolean('jump', true);
setTimeout(function () {
self.entity.rigidbody.applyImpulse(0, self.jumpPower, 0);
} ,200);
}
};
In your PlatformMovement
script, it doesn’t seem to be listening for the 'PlatformMovement:StartGame'
event, which would explain why it doesn’t start when the button is pressed.
One thing to keep in mind for your app level events is the steps you’re taking to route the events to your different controllers. There’s nothing wrong with using app level events to handle this, but it might make it easier to follow if you simplify it a bit. If you’re going to listen for 'GameManager:StartGame'
and then only fire another app level event for other scripts to listen for, those scripts could just listen directly for 'GameManager:StartGame'
leaving you with fewer moving parts.
On the other hand, if you want to have those scripts listen on their own for things (which is perfectly valid for other reasons too), you may want to consider keeping all of your app level event listeners in 'GameManager'
and then firing entity level events to your other managers and listening to them that way. This would look something like:
//GameManager.js
...
GameManager.prototype.initialize = function() {
this.gameStart = false;
this.app.on('GameManager:StartGame', function () {
this.playerManager.fire('startGame', true);
this.platformMovementManager.fire('startGame', true);
}, this);
this.app.on('GameManager:Reset', function () {
this.playerManager.fire('destroy');
this.platformMovementManager.fire('reset');
}, this);
};
...
and then
//player.js
...
Player.prototype.initialize = function () {
this.state = 'idle';
this.falling = false;
this.entity.collision.on('collisionstart', this.onColllision, this);
this.gameStart = false;
this.entity.on('hit', this.onHit, this);
this.entity.on('startGame', this.onStartGame ,this);
};
...
Player.prototype.onStartGame = function(canStart) {
if (canStart) {
this.entity.anim.setTrigger('start');
this.state= 'run';
}
this.gameStart = canStart;
};
Player.prototype.onHit = function() {
this.entity.anim.setTrigger('hit');
};
and
//PlatformMovement.js
...
PlatformMovement.prototype.initialize = function () {
this.speed = -6;
this.parent = this.entity.parent;
this.childCount = this.parent.children.length;
this.entity.on('startGame', this.onStartGame, this);
this.entity.on('reset', this.onReset, this);
};
...
PlatformMovement.prototype.onStartGame = function() {
...
};
PlatformMovement.prototype.onReset = function() {
...
};
...
This is all just a boilerplate example, and I didn’t test this, because I wrote it directly in the forum, but it should put you on the right track for how you may want to organize your scripts and what might be going wrong with them.
I hope this is helpful!