Question
JAVASCRIPT ONLY. NOT C# Implementation. I just need help getting an object to move up and down at the edges of the canvas. Modify the
Modify the HTML file to make the tiger move in the Y direction at 2 pixels every 1/30 of a second. When the tiger reaches either the top or bottom of the screen change its direction (up and down only). When the tiger is moving down the screen, make it 50% transparent. Make the tiger fully opaque when moving up the screen. This is what I've got I can't quite figure out how to make it move up and down. It just gets stuck in an up/down loop.
Many sprites
//--- The sprite object
var spriteObject = { //The x and y source position of the sprite's image //And its height and width sourceX: 0, sourceY: 0, sourceWidth: 64, sourceHeight: 64,
//The x and y position of the sprite on the canvas //as well as its height x: 0, y: 0, width: 64, height: 64, vx: 0, vy: 0,
// boolean set to true by default visible: true, //The sprite's transparency (0.0 to 1.0) alpha: 1
};
//--- The main program
//the canvas and its drawing surface var canvas = document.querySelector(\"canvas\"); var drawingSurface = canvas.getContext(\"2d\");
//An array to store the game sprites var sprites = [];
//Create the background sprite, position it //and push it into the sprites array
var background = Object.create(spriteObject); background.sourceY = 64; background.sourceWidth = 550; background.sourceHeight = 400; background.width = 550; background.height = 400; background.x = 0; background.y = 0; sprites.push(background);
//Create the cat sprite. //Center it on the canvas and push it into the sprites array var cat = Object.create(spriteObject); cat.x = 0; cat.y = 336; sprites.push(cat);
//Create the tiger sprite, position it //and push it into the sprites array var tiger = Object.create(spriteObject); tiger.sourceX = 64; tiger.x = 300; tiger.y = 336; sprites.push(tiger);
//Load the cat's image var image = new Image(); image.addEventListener(\"load\", loadHandler, false); image.src = \"tileSheetWithBackground.png\";
//Arrow key codes var RIGHT = 39; var LEFT = 37;
//Directions var moveRight = false; var moveLeft = false;
//Add keyboard listeners window.addEventListener(\"keydown\", function(event) { switch (event.keyCode) {
case LEFT: moveLeft = true; break;
case RIGHT: moveRight = true; break; } }, false);
window.addEventListener(\"keyup\", function(event) { switch (event.keyCode) {
case LEFT: moveLeft = false; break;
case RIGHT: moveRight = false; break; } }, false);
//Add a mouse listener canvas.addEventListener(\"mousedown\", mousedownHandler, false); window.addEventListener(\"mouseup\", mouseupHandler, false);
function mousedownHandler(event) { //Find the mouse's y position var mouseY = event.pageY - canvas.offsetTop;
//Define the cat's head and feet var top = cat.y; var bottom = cat.y + cat.height;
//Change the velocity based on the mouse's relative position to the cat if (mouseY cat.vy -= 5; } if (mouseY > bottom) { cat.vy += 5; } }
function mouseupHandler(event) { cat.vx = 0; cat.vy = 0; } function tigerTransparency() {
//tiger.y-=1; //Make the tiger move up //tiger.y+=2; //Make the tiger move down
if(tiger.y tiger.y += 2; console.log(\"down\") } else if(tiger.y != 0 && tiger.y tiger.y -= 1; console.log(\"up\") }
if (tiger.y >= 0 && tiger.y tiger.alpha = 1; } else { tiger.alpha = 0.5; } }
function loadHandler() { //Update the sprite as soon as the image has been loaded update(); }
function update() { //Create the animation loop requestAnimationFrame(update, canvas);
//Left if (moveLeft && !moveRight) { cat.vx = -5; } //Right if (moveRight && !moveLeft) { cat.vx = 5; } if (!moveLeft && !moveRight) { cat.vx = 0; }
//Make the cat move cat.x += cat.vx; cat.y += cat.vy;
//Make the tiger move up //tiger.y--; tigerTransparency();
// Screen boundries. cat.x = Math.max(0, Math.min(cat.x + cat.vx, canvas.width - cat.width)); cat.y = Math.max(0, Math.min(cat.y + cat.vy, canvas.height - cat.height)); tiger.y = Math.max(0, Math.min(tiger.y + tiger.vy, canvas.height - tiger.height));
//Render the game render(); }
function render() { //Clear the previous animation frame drawingSurface.clearRect(0, 0, canvas.width, canvas.height);
//Loop through all the sprites and use //their properties to display them if (sprites.length !== 0) { for (var i = 0; i var sprite = sprites[i]; if (sprite.visible) { // checks visibility //Render the sprite's transparency drawingSurface.globalAlpha = sprite.alpha; drawingSurface.drawImage( image, sprite.sourceX, sprite.sourceY, sprite.sourceWidth, sprite.sourceHeight, sprite.x, sprite.y, sprite.width, sprite.height); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started