How to make a sprite with fixed movement distance in Phaser - phaser-framework

I have a grid-based top-down game with arcade physics and I'd like my player to move precisely between grid positions. But how can I do this?
I'm moving the player now on keydown using velocity, then resetting the velocity to 0 on update so there's no inertia.
function update(){
this.player.body.velocity.x = 0
this.player.body.velocity.y = 0
if(this.keyIsLeft){
this.player.body.velocity.x -= 200
}
else if(this.keyIsRight){
this.player.body.velocity.x += 200
}
else if(this.keyIsUp){
this.player.body.velocity.y -= 200
}
else if(this.keyIsDown){
this.player.body.velocity.y += 200
}
But how can I get the player to only move a square (or half square) at a time?
The ideal movement is shown here in this Lolo game, where at any given time the player is on a grid or half-grid space: https://www.youtube.com/watch?v=QQKScIJYUxU&t=5m30s
I can't just set the x and y position or use tweening because I need to keep arcade physics, and those adjustments interfere with them.

Updated:
Here is my updated Answer to solve this case.
I implemented this method based on the example provided by Phaser.
The key part to making this fixed movement happen is to disable the keyboard input while the item is moving.
Let's say if you pressed left key, the player is moving towards left. While the player starts moving, you set a variable notMoving to false, which notifies the update function to not take any cursor inputs. After a timeout, let's say 1000ms, you can set the variable notMoving to true. Then the update function will continue take the cursor input.
Here is the example code snippet:
function create ()
{
cursors = this.input.keyboard.createCursorKeys();
player = this.physics.add.image(400, 300, 'block');
player.setCollideWorldBounds(true);
player.setData('notMoving', true);
}
function setfixedMovement(velocity, direction) {
if (direction === 'up') {
player.setVelocityY(-velocity);
} else if (direction === 'down') {
player.setVelocityY(velocity);
} else if (direction === 'left') {
player.setVelocityX(-velocity);
} else {
player.setVelocityX(velocity);
}
player.setData('notMoving', false);
setTimeout(() => {
player.setData('notMoving', true);
}, 1000);
}
function update () {
if (player.getData('notMoving')) {
player.setVelocity(0);
if (cursors.left.isDown) {
setfixedMovement(300, 'left');
} else if (cursors.right.isDown) {
setfixedMovement(300, 'right');
}
if (cursors.up.isDown) {
setfixedMovement(300, 'up');
} else if (cursors.down.isDown) {
setfixedMovement(300, 'down');
}
}
}
I put the phaser example link in the previous answer. If you edit the example and replace the part of example code with the snippet I provided above, you can see it working as you expected.
Previous Answer:
I think what this phaser example does exactly what you want to do
In short,
in each update, set the velocity to 0,
then detect the check the cursor key status and set the velocity accordingly
the following code is copied from Official Phaser 3 Example
function update ()
{ player.setVelocity(0);
if (cursors.left.isDown)
{
player.setVelocityX(-300);
}
else if (cursors.right.isDown)
{
player.setVelocityX(300);
}
if (cursors.up.isDown)
{
player.setVelocityY(-300);
}
else if (cursors.down.isDown)
{
player.setVelocityY(300);
}
}
Here is the link to the example
https://labs.phaser.io/view.html?src=src\input\keyboard\cursor%20keys.js

Related

Phaser 3: How to detect if there's a group member at location

Okay I have a little game going on Phaser with a slider which the player can move up or down:
As you can see, the slider is on a track which one would assume restricts where the slider can go. At the moment, this isn't the case and the slider can run right off the rail.
How can I detect if there's a slider track in the target location before moving the slider?
Here's where I'm creating the static groups for slider and slider track.
sliders = this.physics.add.staticGroup();
slider_tracks = this.physics.add.staticGroup();
Here's where the objects themselves are being added to the game:
add_slider: function (x, y, data) {
map.add_slider_track(x, y, data);
var slider = sliders.create(x, y, data.direction + '_slider');
for (var key in data) {
slider[key] = data[key];
}
},
add_slider_track: function (x, y, data) {
slider_tracks.create(x, y, data.direction + '_track');
},
And here's the functions which move it:
hitSlider: function (player, slider) {
if (slider.direction == 'vertical') {
if (player.body.onFloor() && player.slamming) {
interaction.moveSliderDown(slider)
} else if (player.body.onCeiling()) {
interaction.moveSliderUp(slider);
}
}
player.slamming = false;
},
moveSliderUp: function (slider) {
slider.setY(slider.y - block_size);
slider.body.position.y = (slider.y - (block_size / 2));
player.setVelocityY(100);
},
moveSliderDown: function (slider) {
slider.setY(slider.y + block_size);
slider.body.position.y = (slider.y - (block_size / 2));
}
I've tried using slider_track.getFirst (https://rexrainbow.github.io/phaser3-rex-notes/docs/site/group/) but it seems to change the location of a given piece of track, not just detect if there's one there.
Just to don't let this question without answer as we normally start a chat, effectively I see in the js/slider_actions.js the solution but I can just say you can use velocity but seriously my level of coding even if I am for a long time in the Phaser community is lower than yours ;)
sliderTrackRight: function (slider) {
track = slider_tracks.children.entries.find(
function (track) {
return (
track.body.y == slider.body.y &&
track.body.x == (slider.body.x + block_size) &&
track.direction == 'horizontal'
)
}
);
return (typeof track != 'undefined');
},

SFML foreground collision

Ive been trying to make an RPG game on SFML and now Im kind of struggling on the collisions. My problem is quiet simple, I have this 2 layers, Background and Foreground. Background just acts as a background image and Foreground png image is supposed to act as the collisions, having the part where the character is supposed to walk completely free (transparent) and keeping the rest of the structures to use as collisions (Background image here: https://imgur.com/gallery/DA3zGtD Im making the floor transparent while keeping the rest). Ive tried using the getGlobalBounds().intesect with the character sprite without any succes. I wanna keep it as simple as possible, here is what I have until now:
#include <iostream>
#include "Piso1.h"
using namespace std;
Piso1::Piso1(){
};
int Piso1::Draw(RenderWindow &window, Event &evento)
{
srand(time(nullptr));
Soundtrack.openFromFile("../Scenes/Piso1/Sounds/Neon District.wav");
Soundtrack.setLoop(true);
Soundtrack.play();
Texture BGTexture;
BGTexture.loadFromFile("../Scenes/Piso1/Graphics/piso1background.png");
Sprite Background;
Background.setTexture(BGTexture);
Background.setScale(8,7.5);
Background.setPosition(BackX,BackY);
Texture FGTexture;
FGTexture.loadFromFile("../Scenes/Piso1/Graphics/piso1foreground.png");
Sprite Foreground;
Foreground.setTexture(FGTexture);
Foreground.setScale(8,7.5);
Foreground.setPosition(BackX,BackY);
Texture ProtaTextura;
ProtaTextura.loadFromFile("../Scenes/Piso1/Graphics/pSprite.png");
IntRect SpriteBx(0,0,34,47);
Sprite Protagonista(ProtaTextura,SpriteBx);
Protagonista.setPosition((window.getSize().x)/2.35,(window.getSize().y)/3);
Protagonista.setScale(3,3);
while (window.isOpen()) {
while (window.pollEvent(evento)) {
switch (evento.type) {
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
EncounterValue = rand()%1000;
if(EncounterValue > 5){
if(evento.key.code == Keyboard::Down) {
BackY -= 10;
Background.move(0,-10);
Foreground.move(0,-10);
//this is my failed attempt
if(Protagonista.getLocalBounds().intersects(Foreground.getLocalBounds()))
{
Collision.openFromFile("../Scenes/Piso1/Sounds/oof.ogg");
Collision.play();
BackY += 10;
Background.move(0, 10);
Foreground.move(0, 10);
}
if(clock1.getElapsedTime().asMilliseconds()>64){
SpriteBx.top = 0;
if (SpriteBx.left == 0)
SpriteBx.left = 34;
else if (SpriteBx.left==34)
SpriteBx.left= 68;
else if (SpriteBx.left== 68)
SpriteBx.left= 102;
else
SpriteBx.left=0;
Protagonista.setTextureRect(SpriteBx);
clock1.restart();
}
break;
}
else if (evento.key.code == Keyboard::Up) {
BackY += 10;
Background.move(0,10);
Foreground.move(0,10);
if (clock1.getElapsedTime().asMilliseconds()>64)
{
SpriteBx.top = 152;
if (SpriteBx.left == 0)
SpriteBx.left = 34;
else if (SpriteBx.left==34)
SpriteBx.left= 68;
else if (SpriteBx.left== 68)
SpriteBx.left= 102;
else
SpriteBx.left=0;
Protagonista.setTextureRect(SpriteBx);
clock1.restart();
}
break;
}
else if(evento.key.code == Keyboard::Left) {
BackX += 10;
Background.move(10,0);
Foreground.move(10,0);
if (clock1.getElapsedTime().asMilliseconds()>64)
{
SpriteBx.top = 53;
if (SpriteBx.left == 0)
SpriteBx.left = 34;
else if (SpriteBx.left==34)
SpriteBx.left= 68;
else if (SpriteBx.left== 68)
SpriteBx.left= 102;
else
SpriteBx.left=0;
Protagonista.setTextureRect(SpriteBx);
clock1.restart();
}
break;
}
else if(evento.key.code == Keyboard::Right){
BackX -= 10;
Background.move(-10,0);
Foreground.move(-10,0);
if (clock1.getElapsedTime().asMilliseconds()>64)
{
SpriteBx.top = 104;
if (SpriteBx.left == 0)
SpriteBx.left = 34;
else if (SpriteBx.left==34)
SpriteBx.left= 68;
else if (SpriteBx.left== 68)
SpriteBx.left= 102;
else
SpriteBx.left=0;
Protagonista.setTextureRect(SpriteBx);
clock1.restart();
}
break;
}
else if(evento.key.code == Keyboard::C){
Soundtrack.stop();
return 1;
}
}
else{
Soundtrack.stop();
return 0;
}
}
window.clear();
window.draw(Foreground);
window.draw(Background);
window.draw(Protagonista);
window.display();
}
}
}
I think whats happening is that when I want to do the if it takes the whole png instead of just the parts I want as collision. I also wanted to try color collision but I really dont know how to implement it, Im kind of new to SFML. Thanks in advance!
If you wish to have proper collision detection between the player and other objects .intersect() is not good enough. It will either make your character permamently stuck in the structure or make it pass through it without player control, which depends on what behavior you code for this condition.
What would work is detecting from which direction the player is approaching the object, and based on that either make him be moved back a bit or set the speed to 0 if current velocity would take him deeper into the structure.
For example, using intersect() as you are, for case of player colliding with a wall from the left:
if(velocity.x > 0 && player.getLocalBounds().intersect(wall.getLocalBounds()) && player.getLocalBounds().left > wall.getLocalBounds().left)
Then you can use this condition to either stop the player (set the velocity.x to 0), set reverse it so he backs off or do any other kind of behavior you'd wish in the event of a collision.
(Note this is not an ideal solution, but it should work and follow similar logic you've implemented.)

How to make something happen when a key is held down in p5.js

I'm trying to make a simple drawing program where the color and size of the line being drawn changes when the 'e' key is held down. However, when I try to use keyPressed, it only executes once, resulting in a single blue dot being drawn instead of changing the whole line. Basically, what I need to know is how to make something happen when a key is held, because it only registers the press once. This is what I have so far:
function keyTyped() {
if (key === 'e') {
if (mouseY < 417) {
fill(0,0,255,100);
ellipse(mouseX,mouseY,5,5);
}
}
}
instead of
function draw() {
if (mouseY < 417) {
noStroke();
fill(0,100);
ellipse(mouseX,mouseY,20,20);
}
}
You could just check from the draw() function, like this:
function draw() {
if (keyIsPressed && key == 'e') {
noStroke();
fill(0,100);
ellipse(mouseX,mouseY,20,20);
}
}
Or you could create a variable that tracks whether the key is being pressed. Set it to true from the keyPressed() function, and set it to false in the keyReleased() function. Then check the variable in the draw() function.
More info can be found in the reference.

Phaserjs, sprite overlap, not working

I am new to Phaserjs, trying to create basic drag-drop style game.
I have created the game and add arcade physics (this.game.physics.arcade.enable(this.orange_outline);)
Currently overlap happen as soon as the edges collide.
I want to detect that my code should trigger when 50% overlap happen. is it possible in phaserjs?
var GameState = {
init:function(){
this.physics.startSystem(Phaser.Physics.ARCADE);
},
create: function () {
this.background = this.game.add.sprite(0, 0, 'background');
this.overlapHappen = false;
this.orange_outline = this.game.add.sprite(459,199,'orange_outline');
this.orange_outline.frame = 2;
this.orange_outline.anchor.setTo(.5);
this.orange_outline.customParams = {myName:'orange_outline',questionImg:'orange'};
this.orange_inner = this.game.add.sprite(150,197,'orange_inner');
this.orange_inner.anchor.setTo(.5);
this.orange_inner.customParams = {myName:'orange_inner',questionImg:'orange',targetKey:this.orange_outline,targetImg:'orange_outline'};
this.orange_inner.frame = 1;
this.orange_inner.inputEnabled = true;
this.orange_inner.input.enableDrag();
this.orange_inner.input.pixelPerfectOver = true;
this.orange_inner.events.onDragStart.add(this.onDragStart,this);
// this.game.physics.enable(this.orange_inner,Phaser.Physics.ARCADE);
this.game.physics.arcade.enable(this.orange_inner);
this.game.physics.arcade.enable(this.orange_outline);
this.orange_inner.events.onDragStop.add(this.onDragStop,this);
},
update: function () {
//this.orange.animations.play('orange_one',1)
},
onDragStart:function(sprite,pointer){
//console.log(sprite.key + " dragged")
},
onDragStop:function(sprite,pointer){
var endSprite = sprite.customParams.targetKey;
//console.log(sprite.customParams);
this.stopDrag(sprite,endSprite)
},
stopDrag:function(currentSprite,endSprite){
if (!this.game.physics.arcade.overlap(currentSprite, endSprite, function() {
var currentSpriteTarget = currentSprite.customParams.targetImg;
var endSpriteName = endSprite.customParams.myName;
if(currentSpriteTarget === endSpriteName){
currentSprite.input.draggable = false;
currentSprite.position.copyFrom(endSprite.position);
currentSprite.anchor.setTo(endSprite.anchor.x, endSprite.anchor.y);
}
console.log(currentSpriteTarget,endSpriteName);
})) {
//currentSprite.position.copyFrom(currentSprite.originalPosition);
console.log('you')
}
}
}
In stopDrag() I am detecting overlap.
You can try to get the amount of horizontal and vertical overlap amount and check if it satisfies a certain threshold. This can be done in additional overlap function callback, that is called processCallback in documentation. As an example:
if (!this.game.physics.arcade.overlap(currentSprite, endSprite, function() {
//your callback !
},function() {
if (this.game.physics.arcade.getOverlapX(currentSprite, endSprite) > currentSprite.width / 2
&& this.game.physics.arcade.getOverlapY(currentSprite, endSprite) > currentSprite.height / 2) {
//Overlaping !
return true;
} else {
//as if no overlap occured
return false;
}
},this) {
//
}
Another way to do this (other than what Hamdi Douss offers) is to resize your body to only take up 50% of the area. This will automatically ensure that collisions/overlap don't occur unless the reduced bodies touch each other.
To view your current body, use Phaser's debug methods
this.game.debug.body(someSprite, 'rgba(255,0,0,0.5)');

How do I detect when the pointer is over a sprite while I'm dragging another sprite?

In my update() function, I use pointerOver() to detect when the pointer is over a sprite. This normally works fine. However, if I happen to be dragging another sprite at the time, the pointerOver() function always returns false.
I thought I'd work around it by getting the location of the pointer and comparing it with the location and bounds of my sprite, but the pointer location is always (-1, -1).
Here's some example code to demonstrate the problem:
var game = new Phaser.Game( 800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
var triangle;
var square;
var isOver;
var pointerX;
var pointerY;
function preload()
{
game.load.image( 'triangle', 'assets/triangle.png' );
game.load.image( 'square', 'assets/square.png' );
}
function create()
{
triangle = game.add.sprite( 100, 100, "triangle" );
triangle.inputEnabled = true;
triangle.input.enableDrag( false, true );
square = game.add.sprite( 100, 200, "square" );
square.inputEnabled = true;
}
function update()
{
isOver = square.input.pointerOver() ? "Yes" : "No";
}
function render()
{
game.debug.text( "Mouse over square: " + isOver, 200, 100 );
game.debug.text( "Pointer: (" + game.input.pointer1.x + ", " + game.input.pointer1.y + ")", 200, 116 );
}
I found this post about using a sprite's input.priorityID: Phaser JS how to stop event Propagation(firing) from textButton.events.onInputDown event to game.input.onDown event?
Using the priorityID fixed it for when the triangle is on top the square and the triangle is not being dragged, but the problem remains when it's being dragged.
How do I detect when the pointer is over a sprite, even when I'm dragging another sprite?
Thanks.
I had a similar issue recently and I hope this will still be of help to you.
I found out that upon initiating a drag, the input on all sprites except for the one you're dragging becomes disabled. That probably happens internally and I couldn't find a way to override it.
What I did is the following: all the objects (in my case instances of Tile) I want to be draggable are held within a Board object, which in turn holds a reference to my Game state. In the Game state I have a flag dragIsActive. In the object class itself I have (TypeScript)
this.tileSprite.events.onDragStart.add(() => {
this.parentBoard.gameState.dragIsActive = true;
});
this.tileSprite.events.onDragStop.add(() => {
this.parentBoard.gameState.dragIsActive = false;
});
The equivalent JavaScript would be
this.tileSprite.events.onDragStart.add(function() {
this.parentBoard.gameState.dragIsActive = true;
});
this.tileSprite.events.onDragStop.add(function() {
this.parentBoard.gameState.dragIsActive = false;
});
called from the object's constructor.
In Game I have the following:
update() {
if (this.dragIsActive) {
var firstTile : Tile = this.game.input.activePointer.targetObject.sprite.parent;
this.board.tiles.forEach((t : Tile) => {
if (firstTile.tileSprite.overlap(t.tileSprite)) {
this.board.tilesToSlide.push(t);
}
});
this.board.tilesToSlide.forEach((tileToSlide) => {
// Do the processing on that array here.
});
}
}
JavaScript (just the forEach loop):
this.board.tiles.forEach(fucntion(t) {
if (firstTile.tileSprite.overlap(t.tileSprite)) {
this.board.tilesToSlide.push(t);
}
});
In my case the check is for two sprites overlapping, but in your case you can check if one just point of interest (e.g. the position of the sprite you're dragging) is inside the bounds of another sprite:
if (secondSprite.getBounds().containsPoint(firstSprite.position)) {
// This should be equivalent to pointerOver() while dragging something.
}
P.S. I hope you're aware that a sprite's position is at the top left of its bounding box unless explicitly set to something else. Set via sprite.anchor.set(x, y) - (0, 0) being top left (the default) and (1, 1) - bottom right.
you can also use this method to see if the point is contained within the sprite bounds.
if( Phaser.Rectangle.contains( sprite.body, this.game.input.x, this.game.input.y) ){
console.log('collide')
}

Resources