javascript

Adding a Toast Message in Phaser

Adding a Toast Message in Phaser

Game Development, javascript, Phaser
A toast message is a text message that fades in and out. It is named a toast message because it is similar to someone raising a glass, making a toast and then lowering the glass again. It gets your attention without interrupting the experience. Here is an example The Code var StateMain = { preload: function() { game.load.image("toastBack", "images/back.png"); game.load.image("btnToast", "images/btnGenerate.png"); }, create: function() { //SET UP TEST BUTTON this.btnToast=game.add.sprite(game.world.centerX,game.height*.25,"btnToast"); this.btnToast.anchor.set(0.5,0.5); this.btnToast.inputEnabled=true; this.btnToast.events.onInputDown.add(this.testToast,this); }, testToast: function() { this.btn...
Faking a Swipe in Phaser

Faking a Swipe in Phaser

javascript, Phaser, Tutorials
One of the advantages native programs like Java for Android or Swift for IOS has over web languages like HTML5 is having built-in detection for detecting swiping. But with a little bit of coding, we can detect the actions of a swipe. For that, we need to break down the action of a swipe The user touches the phone (mouseDown) The user moves a finger across the phone The user lifts their finger(mouseUp) Example Code var StateMain = { preload: function() {}, create: function() { // // // game.input.onUp.add(this.mouseUp, this); game.input.onDown.add(this.mouseDown, this); // // this.text1 = game.add.text(game.world.centerX, game.world.centerY, "swipe left or right!"); this.text1.fill = "#ffffff...
Get the angle between 2 objects in Phaser

Get the angle between 2 objects in Phaser

Game Development, javascript, Phaser, Tutorials
Something that has been very useful for me lately is to be able to get the angle between two objects or get the angle between the mouse and a central character. For example in a game with a spaceship where you want to click and fire, you need to know the angle to be able to turn the ship. The actual math to get the angle is a bit complex, and I won't pretend I understand it, but it has been a very useful snippet for me to have. getAngle: function(obj1, obj2) { // angle in radians var angleRadians = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x); // angle in degrees var angleDeg = (Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x) * 180 / Math.PI); return angleDeg; }, All you need to do to get the angle is to pass two objects like this var angle=this.getAngle(goodGuy,monster); Here is ...
Shuffle an Array

Shuffle an Array

Game Development, javascript, Phaser
As game developers we are always going to need a way to shuffle an array, to randomize it, to mix it up. For example a word in Hangman game or a deck of cards, or puzzle tiles on a scrambled picture type of game. And all we need to do to accomplish that is pick random places in the array and swap the values. Now as a beginning programmer I tried to say something like   array=[1,2]; array[0]=array[1]; array[1]=array[0];   To try to swap the values 1 and 2. But of course the problem here is that array[0] will equal 2 which is fine, but it erases the 1 value in the 0 position before it can be placed into place 2! So our array will be [2,2] instead of [2,1]   The correct way to swap two values in an array is to create a temporary variable to hold the v...
Generate a random string in Javascript/Phaser

Generate a random string in Javascript/Phaser

javascript, Phaser
One of the thing I’ve had to do commonly over the years in making games is to be able to generate a random string. Now lot of times I do this on the server side of code to be able to generate a none, one time use code. But there are times where I have had to do it on the client side maybe to generate a password would be a common use of this, or it could be having to guess what the random letters are in a mastermind type of game. It's a very simple thing to do. All we need to do is to define a set of letters or symbols characters for the program to pick from. We decide on a word length and then loop through that many times pick a random index from the length of the set and then concat a string with that. Here's the outline of the code. make a random string of letters var letters =
BEGINNER CODE – LESSON 2

BEGINNER CODE – LESSON 2

javascript, Tutorials
Variables and you use variables all the time what you know it or not... https://www.youtube.com/watch?v=6iChiXIIdRs Text of Video Follows: Hi, this is William and I'm gonna take the file from our last lesson and change it back to a text file and if you didn't watch the last lesson that's fine, just go ahead and make a new text file on the desktop. I'll open it up and get rid of the alert statement here Now I'm going to tell you a bit about variables and you use variables all the time what you know it or not if you've ever in school had a place where it set name and an underline where to write your name name is the variable and the value is whatever you wrote down on that line. whenever you fill out a form, you're assigning values to variables so I'm going to make a va...
Beginner Code – lesson 1

Beginner Code – lesson 1

javascript, Tutorials
Beginner Code using Javascript Text of video for Beginner Code follows: If you ever thought you might want to be a programmer and thought that you couldn't learn? give me 2 minutes to change your mind with some basic Beginner Code! create a text file on the desktop and this will work on any computer and then in that text file I'm going write just a bit of script and here is one thing I want you know is that we have things in code called tags and most tags have an ending tag and all it is is a word with inside those > < signs and then a slash for the ending tag and this tag is a script tag that tells the computer on the start writing code to pay attention to and then I'm going to but the end tag. So script and end script copy just l...