Skip to content

Instantly share code, notes, and snippets.

@photonstorm
Last active February 21, 2024 05:27
Show Gist options
  • Save photonstorm/46cb8fb4b19fc7717dcad514cdcec064 to your computer and use it in GitHub Desktop.
Save photonstorm/46cb8fb4b19fc7717dcad514cdcec064 to your computer and use it in GitHub Desktop.
Phaser 3 Example
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser-arcade-physics.min.js"></script>
</head>
<body>
<script>
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 }
}
},
scene: {
preload: preload,
create: create
}
};
var game = new Phaser.Game(config);
function preload ()
{
this.load.setBaseURL('http://labs.phaser.io');
this.load.image('sky', 'assets/skies/space3.png');
this.load.image('logo', 'assets/sprites/phaser3-logo.png');
this.load.image('red', 'assets/particles/red.png');
}
function create ()
{
this.add.image(400, 300, 'sky');
var particles = this.add.particles('red');
var emitter = particles.createEmitter({
speed: 100,
scale: { start: 1, end: 0 },
blendMode: 'ADD'
});
var logo = this.physics.add.image(400, 100, 'logo');
logo.setVelocity(100, 200);
logo.setBounce(1, 1);
logo.setCollideWorldBounds(true);
emitter.startFollow(logo);
}
</script>
</body>
</html>
@designbyadrian
Copy link

BlendMode 'ADD', doesn't seem to work in Chrome.

@frob
Copy link

frob commented Aug 20, 2018

ADD doesn't work in Firefox either. I changed it to NORMAL and it works.

@frob
Copy link

frob commented Aug 20, 2018

Looks like none of the WebGL modes that are listed here

Copy link

ghost commented Aug 23, 2018

For anyone else experiencing similar issues with the blend modes, blendMode: 'ADD' and blendMode: Phaser.BlendModes.ADD both work for me on both Firefox and Chrome. I managed to get the sky and red particle asset from https://github.com/photonstorm/phaser-examples/tree/master/examples/assets and downloaded 'assets/sprites/phaser.png' instead of 'phaser-3-logo.png'. Hopefully this information helps someone.

@aR-Dee
Copy link

aR-Dee commented Sep 5, 2018

Seems that when you try to load local images and later on this.load.setBaseURL it seems to append the URL even to previously loaded ones. Description of this function states otherwise (that it adds URL "...from this point on..."). Anyone had such issue?

@adrian-castravete
Copy link

For those having problems with the ADD blend mode. I downloaded the phaser.js from the main site and the files from those locations. I doubt the files have anything to do with the problem at hand, because everything worked fine when I used add as the blend mode (all lowercase).

@bartread
Copy link

bartread commented May 5, 2019

Hey, this is a nice example to start off with, but can you update line 29 from:

this.load.setBaseURL('http://labs.phaser.io');

to

this.load.setBaseURL('https://labs.phaser.io');

please?

The reason is that, at present, if you've served helloworld.html over an HTTPS connection (I do all dev work over HTTPS, even locally), you'll get resource blocking errors, and none of the images will load (tested in Google Chrome). OTOH if you load the images over HTTPS I believe this will not cause a problem if helloworld.html itself is only served over HTTP.

You can see I've done this at https://gist.github.com/bartread/95b06e2d295f7158b6c110e360d89fbc but, of course, gists don't support PRs or I would have created one. I haven't made any other modifications though, so you could simply copy and paste the entire file content from my gist.

Thanks!

@tylerlong
Copy link

WebAudioSoundManager.js:118 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu

@mrwolferinc
Copy link

WebAudioSoundManager.js:118 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu

I think this happened because there were no audio files preloaded or used, so the AudioContext was useless.

@eddie120678
Copy link

I was doing something else and thought I was loading phaser wrong. I typed this example out and it ran just fine for me.

@Noah670
Copy link

Noah670 commented Jul 31, 2021

Thanks!

@marbelyoo
Copy link

Why this. ? I'm a beginner sorry. can you teach me?
hmm

@designbyadrian
Copy link

Why this. ? I'm a beginner sorry. can you teach me?
hmm

You’re trying to call a function “add” on something that doesn’t exist. That’s all anyone can tell you as long as you don’t share the code you’ve written. Please always share what you’ve written when asking for programming help.

@tkwk9
Copy link

tkwk9 commented Aug 18, 2023

Hey, this is a nice example to start off with, but can you update line 29 from:

this.load.setBaseURL('http://labs.phaser.io');

to

this.load.setBaseURL('https://labs.phaser.io');

please?

The reason is that, at present, if you've served helloworld.html over an HTTPS connection (I do all dev work over HTTPS, even locally), you'll get resource blocking errors, and none of the images will load (tested in Google Chrome). OTOH if you load the images over HTTPS I believe this will not cause a problem if helloworld.html itself is only served over HTTP.

You can see I've done this at https://gist.github.com/bartread/95b06e2d295f7158b6c110e360d89fbc but, of course, gists don't support PRs or I would have created one. I haven't made any other modifications though, so you could simply copy and paste the entire file content from my gist.

Thanks!

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment