How can I not bounce on the item I am collecting?

Ball class:

import { addBounceTween } from './../helpers'

export default class Ball extends Phaser.Physics.Arcade.Sprite
{
	/**
	* @param {Phaser.Scene} scene
	* @param {number} x
	* @param {number} y
	* @param {string} texture
	*/
	constructor(scene, x, y, texture)
	{
		super(scene, x, y, texture)
		this.setScale(1.4)
		addBounceTween.call(scene, this)
	}
}

My ball:

this.balls = this.physics.add.staticGroup({classType: Ball, allowGravity: false, immovable: true })
this.balls.create(1420, 773, 'ball')

My player:

this.player = this.physics.add.sprite(100, 600, 'player')

My collider:

this.physics.add.collider(this.player, this.balls, this.handleCollectBall, undefined, this)

I can touch the top of the ball, but that shouldn’t be.

Use add.overlap(…) instead.

It doesn’t help me. In this case, I can jump by simply touching the element …

Show your jump code?

Probably avoid using body.touching.down.

Player jump:

this.player.setVelocityY(-450)

I don’t understand the problem.

What is happening now? What do you want to happen instead?

Can you post the whole scene code?

Main game:

import Ball from './../../game/Ball'

preload(){
	this.load.image('ball', 'assets/ball.png')
	this.load.spritesheet(`player`, `assets/player/player.png`, { frameWidth: 79, frameHeight: 233})
}

create(){
	this.balls = this.physics.add.group({classType: Ball, allowGravity: false, immovable: true})
	this.balls.create(1420, 773, 'ball')
	this.balls.create(1680, 743, 'ball')
	this.balls.create(2280, 773, 'ball')
	this.balls.create(2700, 743, 'ball')

	this.player = this.physics.add.sprite(100, 600, `player`)
	this.player.setBodySize(79, 233)
	this.player.setOffset(0, 0)

	this.physics.add.collider(this.player, this.balls, this.handleCollectBall, undefined, this)
}

update(){
	
}

handleCollectBall(player, ball) {
	ball.destroy()
}

Ball class:

export default class Ball extends Phaser.Physics.Arcade.Sprite
{
	/**
	* @param {Phaser.Scene} scene
	* @param {number} x
	* @param {number} y
	* @param {string} texture
	*/
	constructor(scene, x, y, texture)
	{
		super(scene, x, y, texture)
		this.setScale(1.4)
		addBounceTween.call(scene, this)
	}
}

export function addBounceTween(object) {
    return this.tweens.add({
        targets: object,
        duration: 800,
        y: '-=8',
        yoyo: true,
        repeat: -1,
        ease: 'Sine.easeInOut'
    })
}

What about code for player movement?

I check if it is still on the ground with:

this.player.body.touching.down

Use body.blocked.down or body.onFloor() instead.

1 Like