Strange behavior of animation in Easeljs - javascript

i'm only began to learn Javascript, so i have some troubles with animation in EaselJs. I'm trying to move object on mouse click (that's ok as i've expected), but when i'm try to add animation of movement in proper way, my animation doesn't acts, just stops at one frame when object moves. Here's the code of main.js and example of problem: http://rustem.ucoz.com/onMouseRun/index.html
var canvas;
var stage;
var imgCharRun = new Image();
var imgCharIdle = new Image();
var bmpAnimation;
var canvasx;
var isClicked = false;
function init() {
canvas = document.getElementById('canvas');
canvasx = canvas.offsetLeft;
imgCharRun.src = "monsterRun.png";
imgCharIdle.src = "monsterIdle.png";
startGame();
}
function startGame(){
stage = new createjs.Stage(canvas);
stage.enableMouseOver(10);
var spriteSheet = new createjs.SpriteSheet({
images: [imgCharRun, imgCharIdle],
frames: {width: 64, height: 64, regX: 32, regY: 32},
animations: {
walk: [0, 9, "walk"],
idle: [10, 20, "idle"]
}
});
bmpAnimation = new createjs.BitmapAnimation(spriteSheet);
bmpAnimation.gotoAndPlay ("idle");
bmpAnimation.scaleX = 1;
bmpAnimation.x = canvas.width/2;
bmpAnimation.y = canvas.height-100;
bmpAnimation.currentFrame = 9;
stage.addChild(bmpAnimation);
createjs.Ticker.addListener(window);
createjs.Ticker.useRAF = true;
createjs.Ticker.setFPS(30);
}
document.onmousedown = function(e) {
isClicked = true;
window.mousex = e.clientX-canvasx;
}
function isMouseClicked() {
if (isClicked){
moveToMouse();
}
}
function moveToMouse() {
if(bmpAnimation.x<mousex) {
bmpAnimation.x+=5;
bmpAnimation.gotoAndPlay("walk");
bmpAnimation.scaleX = -1;
}
if(bmpAnimation.x>mousex) {
bmpAnimation.x-=5;
bmpAnimation.gotoAndPlay("walk");
bmpAnimation.scaleX = 1;
}
}
function tick() {
stage.update();
isMouseClicked();
}
Please, help me with this problem:

You are calling gotoAndPlay("walk") every frame while the mouse is down, so it constantly sets the animation to the first frame of the walk animation. You need a flag to indicate if the animation is running already, and ensure you only call it one time.

Problem is solved, thanks. Here is what i wanted: http://rockfor.us/onMouseRun/index.html

Related

Phaser bitmap multiply does not affect sprites

Hi I'm trying to make a basic lightning system using bitmap multiply in PhaserJS. How ever I'm having trouble with the fact that the bitmap does not darken my sprites. How would I achieve this?
I create the bitmap like so in the games create function.
// Shadow
this.bitmap = this.game.add.bitmapData(
this.game.width+100,
this.game.height+100
);
this.lightBitmap = this.game.add.image(0, 0, this.bitmap);
this.lightBitmap.blendMode = Phaser.blendModes.MULTIPLY;
I then fill it and paint it every frame in the update function.
this.bitmap.fill(20, 20, 20, 1);
this.lightBitmap.x = this.camera.x-50;
this.lightBitmap.y = this.camera.y-50;
this.bitmap.dirty = true;
Here is an image that shows what happens. I painted the shadow on half screen only to illustrate that it does not darken the sprites. The dirt tiles and the purple player are the sprites I want it to affect.
How can I make it darken the sprites as well?
That's probably because of the order you are adding your images. You should add the shadow as the last one if you want to affect to all your elements. Or you can always use bringToTop method.
Take a look at this example:
var game = new Phaser.Game(500, 500, Phaser.AUTO, 'game');
var mainState = {
preload:function(){
var dataURI = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAAJdEVYdFRpdGxlAENhcobOmFIAAAALdEVYdEF1dGhvcgBMb3Jpw/Hj0QAAAB90RVh0RGVzY3JpcHRpb24Ac2lkZS12aWV3IG9mIGEgY2FyIB3sOscAAB2qSURBVHic7Zx5mB1Vteh/a++qOnX69Dyk6XRIQiAQBgljLnIJIOhTQHB4gID4QMWrXn1ehw9HvE+v914VfQ6fT5wuguJ4H6iIEzKIDBoZboAECCQk6czpeTp9hqq99/uj6nSf7nQnYQr9va/X962v6uxTw671q73X2mvvc8Q5x5zMHlEvdwXmZLLMAZllMgdklskckFkmc0BmmcwBmWUyB2SWyRyQWSZzQGaZzAGZZTIHZJbJHJBZJnNAZpnMAZllMgdklskckFkmc0BmmcwBmWUyB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kIAAAAAElFTkSuQmCC'
var data = new Image();
data.src = dataURI;
game.cache.addImage('car', dataURI, data);
var dataURI2 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAoHSURBVGhD7VgHVFRXGlajLEHjKhEVXOkMMCBlaAIajaxRbHSBUcGGLaKCGGSxYEERhMEuiN1o7BU1gIgFCyKi2PsqdhETTbKrsl/+/82MDDjuWuLZ3XPmO+c797475X3fvf//3/teHQ000EADDTTQQIN3hiFRRNQjNiLWI/5fQJ/Yy8bw89TwrgZHB/oaPZsZ5/hSNsW5YmKU3d+jh4nLhoSJjoT6m+zs3MFgdVtJs3Qby8aTDVpoj6DfSYldiW5ENt+K+GdifeInRwtiN2tDnRmBXzUviAsxfrLyOxscmeuKR9le2CJzw5VDPsCj/sDdMPzrXjhelofh15t9UXlFivtng3HjZCDOHvRFYXY37P2hMzZkdcTSdM+XmakeT+fMcCuPHG5zycqiyfGG2vVymjb8bJz8th8PXaKXmb52go+HXl5MkFHFshgbHJK54OIyD1xd6YnzS91RmuGOaxs6IHOiPQq3dsHTCyF4WNYbj84yg/H4XDCe0FjlpVD8RIaeXe+Dl3fCgKeDgOcReHarH47l9MTSVA8kjrGFbKQVDqQ6Y1p/M9D93QUl74k/ETvq1Kszwd+92Y8TpcYP1sXZ4sQCN9xc0w6317bHlRWeOJPpjuKFbjTuKpD719Z3wMLxbVCw3gtPyoLw4FRgTZYGofJ8MP55ow9+uSbFhUI/bMz6GonjHBAbboGZA82xPs4GxXPlk3Quyx0XqP2rRPcEaXq/XHN1/HLP0P6WiOhtioQIS6RFWiN5iAhzR4iwKNISq2PE2DXFDvuTJThB4VSW4YYLtCJ84wc7OiEz3h4HfvBCZWkA7h73Jfqhgvq/XAxGxZkgFO/uhpUyTyRE2iC2nxlSh1hgx2Q7lC50FUSfzmgrTBqziCbowl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LRqXuylR10Wz61smAWkXze/Zq6vnUISJHIlHwn9pg1G+3g23zvI2+BSvNTk+fxIK2yb4oDD6fQcToYur5C/ArpEBvmxlQ+ANR+Y5OQNlV/n8BGFQ4ZNcALn06Fxf4oEq78T0+n2L8/b2TbZSLf1I34uCPgE0CK2JrZvpFV3gNhIJ6mrs+6W/t/ol8YGGz9OpcfODbQnFFD4sBk2dpUegC6vkL/nYtOHyDwb4cTl2eeVmEpHEz9PvaIWTRvwizczvtF/E82JzsQQs1bakzvaNVkj7aR/PCrA6M7MQeZVq2JthUTnVeQHqyVjLDGwi/49WxOdDPpNB/6D/3U0IYqJPoZ62uPcrL9YYdxCa4tW/brhNMZvLDXQQAMNNNBAAw3Uok6d3wEMwmSh7B0m8QAAAABJRU5ErkJggg==';
var data2 = new Image();
data2.src = dataURI2;
game.cache.addImage('wood', dataURI2, data2);
},
create:function(){
game.stage.backgroundColor = "#4488AA";
this.bitmap = game.add.bitmapData(
200,
200
);
this.lightBitmap = game.add.image(0, 0, this.bitmap);
this.lightBitmap.blendMode = Phaser.blendModes.MULTIPLY;
game.physics.startSystem(Phaser.Physics.ARCADE);
this.car =this.game.add.sprite(game.world.centerX,game.world.centerY,'car');
this.car.anchor.setTo(0.5,0.5);
game.physics.arcade.enable(this.car);
game.physics.arcade.enable(this.car);
platforms = game.add.group();
platforms.enableBody = true;
var ground = platforms.create(100,100, 'wood');
var ground2 = platforms.create(400,400, 'wood');
ground.body.immovable = true;
ground2.body.immovable = true;
},
update:function(){
this.car.body.velocity.x =0;
this.car.body.velocity.y = 0;
if(game.input.keyboard.isDown(Phaser.Keyboard.LEFT)){
this.car.body.velocity.x = -100;
}
if(game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
this.car.body.velocity.x = 100;
}
if(game.input.keyboard.isDown(Phaser.Keyboard.UP)){
this.car.body.velocity.y = -100;
}
if(game.input.keyboard.isDown(Phaser.Keyboard.DOWN)){
this.car.body.velocity.y = 100;
}
this.bitmap.fill(50, 50, 50, 1);
this.lightBitmap.x = this.car.body.x-50;
this.lightBitmap.y = this.car.body.y-50;
this.bitmap.dirty = true;
game.world.bringToTop(this.lightBitmap);
},
};
game.state.add('main', mainState);
game.state.start('main');
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.4/phaser.min.js"></script>
<div id="game"></div>

Animate moving an image in canvas

Im new to using canvas but wonder if i could get some help or advice on where to start with this. i currently have a circle drawn on canvas and have tried multiple ways i thought might work but cant get my head round it. When searching online i can only really find help where the shapes are drawn in canvas itself.
Here is what i have so far: JSFiddle
JavaScript:
var one = document.getElementById("canvOne");
var canvOne = one.getContext("2d");
var img = new Image();
img.onload = function(){
canvOne.drawImage(img, 10, 10);
};
img.src = "img/circle.jpg";
function oneStart(){
for(var i = 0; i < 300; i++){
img.style.left = i + 'px';
setTimeout(oneStart, 1000);
}
};
can anyone give me a hand with this please?
jsFiddle : http://jsfiddle.net/8eLL7L5L/3/
javascript
//canvas one
var one = document.getElementById("canvOne");
var canvOne = one.getContext("2d");
var img = new Image();
var animate = false;
var circlePosX = 10;
var circlePosY = 10;
img.onload = function () {
canvOne.drawImage(img, circlePosX, circlePosY);
};
img.src = "http://www.alexvolley.co.uk/other/circle.jpg";
var start = function()
{
animate = true;
}
var stop = function()
{
animate = false;
}
setInterval(function () {
// Only update circle position if animate is true
if (animate) {
circlePosX += 1;
canvOne.fillStyle = "#FFF";
canvOne.fillRect(0, 0, one.width, one.height);
canvOne.drawImage(img, circlePosX, circlePosY);
}
}, 3);
All I have done is created 3 variables and added a few little functions, the circle's XPos and YPos are now stored so we can access them, I have also created a bool variable animate which is used to check if we should animate the circle.
The start function simply sets animate to true
The stop function simple sets animate to false
the setInterval just keeps running the same code over and over every 3 miliseconds, all this does is clears the canvas and then redraws the circle. If we don't clear the canvas you would see multiple circles appear when animate is true.
I hope this helped

Mouse position is only read on the first frame

i have been having trouble with reading a mouse position on a canvas. The code is working (semi) correctly as it reads the position when clicking he canvas in IE but only on one frame, in chrome it is just displaying the value as 0.
Here is the full code:
<script>
var blip = new Audio("blip.mp3");
blip.load();
var levelUp = new Audio("levelUp.mp3");
levelUp.load();
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
context.font = '18pt Calibri';
context.fillStyle = 'white';
//load and draw background image
var bgReady = false;
var background = new Image();
background.src = 'images/background.jpg';
background.onload = function(){
bgReady = true;
}
var startMessage = 'Click the canvas to start';
//load plane image
var planeReady = false;
var planeImage = new Image();
planeImage.src = 'images/plane.png';
planeImage.onload = function() {
planeReady = true;
}
//load missile image
var missileReady = false;
var missileImage = new Image();
missileImage.src = 'images/missile-flipped.gif';
missileImage.onload = function() {
missileReady = true;
}
//initialise lives and score
var score = 0;
var lives = 3;
var missilesLaunched = 0;
var missileSpeed = 5;
var level = 1;
var missileX = 960;
var missileY = Math.random() * 500;
if (missileY > 480) {
missileY = 480;
}
function getMousePos(canvas, event) {
return {
x: input.x - rect.left,
y: input.y - rect.top
};
}
function update_images(event) {
var pos = getMousePos(canvas.getBoundingClientRect(), mouseInput);
planeImage.y = pos.y;
missileX = missileX - missileSpeed;
if (missileX < - 70) {
missilesLaunched++;
missileX = 960;
missileY = Math.random() * 500;
if (missileY > 480) {
missileY = 480;
}
blip.play();
score = missilesLaunched;
if (score % 5 == 0) {
missileSpeed = missileSpeed + 2;
level++;
levelUp.play();
}
}
}
function reload_images() {
if (bgReady = true) {
context.drawImage(background, 0, 0);
}
if (planeReady = true) {
context.drawImage(planeImage, 10, planeImage.y);
}
if (missileReady = true) {
context.drawImage(missileImage, missileX, missileY);
}
context.fillText('Lives: ' + lives, 200, 30);
context.fillText('Score: ' + score, 650, 30);
context.fillText('Level: ' + missileSpeed, 420, 30);
context.fillText('Position: ' + missileImage.y, 420, 70);
}
function main(event) {
var mouseInput = { x: 0, y: 0 };
document.addEventListener("mousemove", function (event) {
mouseInput.x = event.clientX;
mouseInput.y = event.clientY;
});
update_images(event);
reload_images();
if (lives > 0) {
window.requestAnimationFrame(main);
}
else {
}
}
function start() {
context.drawImage(background, 0, 0);
context.fillText('Click the canvas to start', 350, 250);
function startMain(event) {
game.removeEventListener("click", startMain);
main(event);
}
canvas.addEventListener("mousedown", startMain);
}
start();
</script>
Joe, you should actually be capturing the mouse position every time you click...
...but you're actually also starting a new game (without stopping the old one), every time you click, too.
First problem: starting game engine several times to draw on the same instance of the canvas
Solution:
In your start function, you need to remove the mousedown event listener, after you've triggered it.
function start () {
// ... other setup
function startMain (event) {
canvas.removeEventListener("click", startMain);
main(event);
}
canvas.addEventListener("click", startMain);
}
Now it will only listen for the first click, before starting, and will only start once.
Second Problem: mouse doesn't update as expected
Solution: two issues here...
...first, you are passing event into main on first call...
...after that, you're passing main into requestAnimationFrame.
requestAnimationFrame won't call it with an event, it will call it with the number of microseconds (or ms or some other unit as a fractional precision of ms) since the page was loaded.
So the first time you got main({ type: "mousedown", ... });.
The next time you get main(4378.002358007);
So lets refactor the startMain we had above, so that main never ever collects an event, just a time.
function startMain ( ) {
canvas.removeEventListener("click", startMain);
requestAnimationFrame(main);
}
The next problem is that even if you were getting just events, you're only ever capturing a click event (which as we mentioned earlier, fires a new copy of the game logic).
Your solution is to separate the code which catches mouse events from the code which reads mouse position.
var mouseInput = { x: 0, y: 0 };
document.addEventListener("mousemove", function (event) {
mouseInput.x = event.clientX;
mouseInput.y = event.clientY;
});
function getMousePos (rect, input) {
return {
x : input.x - rect.left,
y : input.y - rect.top
};
}
// currently in updateImages (should not be there, but... a different story)
var pos = getMousePos(canvas.getBoundingClientRect(), mouseInput);
You've got other problems, too...
You're calling getMousePos and passing in game at the moment. I don't see where game is defined in your JS, so either you're making game somewhere else (begging for bugs), or it's undefined, and your app blows up right there.
You should really be building this with your console / dev-tools open, in a hands-on fashion, and cleaning bugs in each section, as you go.

Src Canvas Flickering

I am trying to figure out how to switch images on a canvas without a small time of blankness in between.
To make my point here is an extreme example of this. In this program, when your spacebar is held down it redraws the image circle.png every milisecond. Because this is so fast the image dissappears.
I did try to preload my image but it didn't help.
Here is my full code:
// Access Canvas
var canvas = document.getElementById("gameBoard");
var ctx = canvas.getContext("2d");
// preload image
var circleReady = false;
var circleImage = new Image();
circleImage.onload = function () {
circleReady = true;
};
circleImage.src = "images/Circle.png";
// Game objects
var circle = {
};
// circle location
circle.x = canvas.width / 2;
circle.y = canvas.height / 2;
// Keyboard events
var keysDown = {};
addEventListener("keydown", function (e) {
keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function (e) {
delete keysDown[e.keyCode];
}, false);
// Update Objects
var update = function () {
if (32 in keysDown) { // Player space pressed
circleImage.src = "images/Circle.png"; //re-draws image
}
};
// Draws Everything
var render = function () {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0,0,600,609);
if (circleReady) {
ctx.drawImage(circleImage, circle.x, circle.y);
}
};
// The main loop
var main = function () {
update();
render();
};
// Starts Function
var then = Date.now();
setInterval(main, 1); // Execute as fast as possible
You are reloading the image in the update function which is causing your delay:
circleImage.src = "images/Circle.png"; //re-draws image
All you have to do is drawImage without reloading the image:
ctx.drawImage(circleImage, circle.x, circle.y);
Here is an example of an image loader that loads all your images before execution begins:
var imageURLs=[]; // put the paths to your images here
var imagesOK=0;
var imgs=[];
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house1.jpg");
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house2.jpg");
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house3.jpg");
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house4.jpg");
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house5.jpg");
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house6.jpg");
loadAllImages(start);
function loadAllImages(callback){
for (var i=0; i<imageURLs.length; i++) {
var img = new Image();
imgs.push(img);
img.onload = function(){
imagesOK++;
if (imagesOK>=imageURLs.length ) {
callback();
}
};
img.onerror=function(){alert("image load failed");}
img.crossOrigin="anonymous";
img.src = imageURLs[i];
}
}
function start(){
// all your images are fully loaded so begin your app
}
You can have two canvases, one for each image. When you want to switch. Remove or hide the one in the front, leaving (instantly) the one in the back.
Per your comment, here is an example of 26 letters of the alphabet. We only keep two canvases at a time. Each time the user clicks whe add a new only, and make the hidden one visible, with no delay.
function createCanvas(letter) {
var canvas = document.createElement('canvas');
canvas.id = letter;
canvas.style.display = 'none';
document.getElementById('container').appendChild(canvas);
var image = new Image();
image.src = 'http://icons.iconarchive.com/icons/iconicon/alpha-magnets/128/Letter-'+letter+'-icon.png';
image.onload = function() {
canvas.getContext('2d').drawImage(this, 0, 0);
};
return canvas;
}
createCanvas('a').style.display = '';
createCanvas('b');
var prevCharCode = 'a'.charCodeAt(0);
var charCode = 'b'.charCodeAt(0);
document.addEventListener('click', function() {
document.getElementById(String.fromCharCode(charCode)).style.display = '';
document.getElementById('container').removeChild(
document.getElementById(String.fromCharCode(prevCharCode))
);
prevCharCode = charCode;
if(++charCode > 'z'.charCodeAt(0)) {
charCode = 'a'.charCodeAt(0);
}
createCanvas(String.fromCharCode(charCode));
});

How can I redraw a circle in html5 with retaining the visibility of the previous circle path

I'm tring to draw an expanding and contracting ring that pulses using javascript and html 5. The problem is (using the below code) when the path is repainted the current path remains visible and just gets fatter. Anybody know why this could be?
function drawOuterInfoCircle(){
var number = 25;
var increase = true;
function draw(){
if(increase==true){
number++
//alert('increase');
if(number==30){
increase=false
}
}
if(increase==false){
number--;
//alert('decrease');
if(number==25){
increase=true
}
}
var drawingCanvas = document.getElementById('canvas_circle');
var drawingContext1 = drawingCanvas.getContext('2d');
drawingContext1.strokeStyle = "#990000";
drawingContext1.lineWidth = 12;
drawingContext1.beginPath();
drawingContext1.arc(100, 100, number, 0, Math.PI*2, true);
drawingContext1.closePath();
drawingContext1.stroke();
}
setInterval(draw,100);
}
I've tried clearing the canvas with the following (from post 3088229) tna
drawingContext1.fillStyle = 'rgba(0,0,0,0)';
drawingContext1.fill();
the fiddle
You are filling with opacity 0, try rgba(0,0,0,1)
before each ring drawing erase the older canvas and redraw on it,
function draw(){
var drawingCanvas = document.getElementById('canvas_circle');
var drawingContext1 = drawingCanvas.getContext('2d');
drawingContext1.clearRect(0,0,drawingCanvas_width,drawingCanvas_height);
if(increase==true){
number++
//alert('increase');
if(number==30){
increase=false
}
}
if(increase==false){
number--;
//alert('decrease');
if(number==25){
increase=true
}
}
//var drawingCanvas = document.getElementById('canvas_circle');
//var drawingContext1 = drawingCanvas.getContext('2d');
drawingContext1.strokeStyle = "#990000";
drawingContext1.lineWidth = 12;
drawingContext1.beginPath();
drawingContext1.arc(100, 100, number, 0, Math.PI*2, true);
drawingContext1.closePath();
drawingContext1.stroke();
}

Categories

Resources