Created
April 26, 2024 19:00
-
-
Save remarkablemark/1810140df5b46cc3a4708527cb0bd64e to your computer and use it in GitHub Desktop.
Phaser: Toss the Turtle prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Example extends Phaser.Scene | |
{ | |
preload() { | |
this.load.image('backdrop', 'assets/pics/platformer-backdrop.png'); | |
this.load.image('cannon_head', 'assets/tests/timer/cannon_head.png'); | |
this.load.image('cannon_body', 'assets/tests/timer/cannon_body.png'); | |
this.load.spritesheet('chick', 'assets/sprites/chick.png', { frameWidth: 16, frameHeight: 18 }); | |
} | |
create() { | |
this.anims.create({ key: 'fly', frames: this.anims.generateFrameNumbers('chick', [ 0, 1, 2, 3 ]), frameRate: 5, repeat: -1 }); | |
// this.add.image(320, 256, 'backdrop').setScale(2); | |
const backdrop = this.add.tileSprite(0, 0, game.config.width, game.config.height, 'backdrop') | |
.setScale(2); | |
// .setScrollFactor(0); | |
this.backdrop = backdrop; | |
const cannonHead = this.add.image(130, 416, 'cannon_head').setDepth(1); | |
const cannon = this.add.image(130, 464, 'cannon_body').setDepth(1); | |
const chick = this.physics.add.sprite(cannon.x, cannon.y - 50, 'chick') | |
.setScale(2) | |
.setDrag(20); | |
this.chick = chick; | |
const graphics = this.add.graphics({ lineStyle: { width: 10, color: 0xffdd00, alpha: 0.5 } }); | |
const line = new Phaser.Geom.Line(); | |
chick.setCollideWorldBounds(true, 0, 0.5); | |
this.physics.world.checkCollision.up = false; | |
this.physics.world.checkCollision.left = false; | |
this.physics.world.checkCollision.right = false; | |
let angle = 0; | |
this.input.on('pointermove', (pointer) => { | |
angle = Phaser.Math.Angle.BetweenPoints(cannon, pointer); | |
cannonHead.rotation = angle; | |
Phaser.Geom.Line.SetToAngle(line, cannon.x, cannon.y - 50, angle, 128); | |
graphics.clear().strokeLineShape(line); | |
}); | |
this.input.on('pointerup', () => { | |
chick.enableBody(true, cannon.x, cannon.y - 50, true, true); | |
chick.play('fly'); | |
this.physics.velocityFromRotation(angle, 600, chick.body.velocity); | |
this.cameras.main.startFollow(chick, false, 1, 1); | |
// this.cameras.main.setBounds(0, 0, undefined, this.game.config.height); | |
// this.cameras.main.setDeadzone(0, 0); | |
// this.cameras.main.setFollowOffset(0, 100); | |
}); | |
} | |
update() { | |
if (this.chick.y > this.game.config.height) { | |
this.backdrop.tilePositionY = this.chick.y; | |
} | |
this.backdrop.tilePositionX = this.chick.x; | |
// this.backdrop.tilePositionY = this.cameras.main.y; | |
} | |
} | |
const config = { | |
type: Phaser.AUTO, | |
width: 640, | |
height: 512, | |
parent: 'phaser-example', | |
pixelArt: true, | |
physics: { | |
default: 'arcade', | |
arcade: { | |
gravity: { y: 300 } | |
} | |
}, | |
scene: Example | |
}; | |
const game = new Phaser.Game(config); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment