Set 7: Events and Animation IT452 Advanced Web and Internet Systems.

Slides:



Advertisements
Similar presentations
JavaScript Chapter 6 Note new scoring. spin.js (page 67) function spin() function spin() { var obj_style = document.getElementById("d1").style; var obj_style.
Advertisements

Microsoft® Small Basic
Bring Life to Your Web Pages with JavaScript and HTML5 Ole Ildsgaard Hougaard -
The jQuery library. What is jQuery ? A javascript lightweight library Is very easy to use Is powerful Is cross-browser compatible Downloadable from jQuery.com,
CSS Layout Crash Course An Advance CSS Tutorial. Inline vs. Block Many HTML elements have a default display setting of Block. Block elements take up the.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
A Christmas Scratch game
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Information Technology Center Hany Abdelwahab Computer Specialist.
SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2010SCM-CityU1.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
INTRODUCTION TO SCRATCH. About Me Resources Scratch Website Learn Scratch Washington-Lee Computer.
 As you complete the assignment, go in order as the earlier bulleted parts provide an easier opportunity for points than the later.  This project is.
Guide to Programming with Python
Jquery IS JQuery Jquery Example of mouseover event that shows a submenu when menu selected: $(‘#menu’).mouseover(function() { $(‘#submenu’).show();
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Introduction to TouchDevelop
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Google Sketchup Lab Mr. Garner Tech Ed Lime Kiln MS.
Locally Edited Animations We will need 3 files to help get us started at
JQuery Adding behaviour…. Lecture Plan Review of last lesson Adding behaviour –click, mouseover Animation –fade, slideDown Navigation –parent, find, next.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Programming Games Show project. Refresher on coordinates. Scaling, translation. HTML5 logo. Refresher on animation. Bouncing ball. Homework: Do your own.
Working with Objects. Objectives Create an object Transform an object Arrange and lock an object Step and repeat an object Use Live Distribute Use the.
Chapter 5 Quick Links Slide 2 Performance Objectives Understanding Framesets and Frames Creating Framesets and Frames Selecting Framesets and Frames Using.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
GAME:IT Helicopter Objectives: Review skills in making directional sprites Create objects that shoot and destroy for points Create random enemies on the.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
Game Maker Terminology
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
This is what we want to draw Extrude Mirror Round Edges.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Intro to Action Script 9 "The games of a.
Mathematical Expressions, Conditional Statements, Control Structures
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009 Intro to Action Script 9 "The games of a people reveal.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
11/12/2015 Box Model Reed Crouch. 11/12/2015  HTML elements can be considered as boxes. In CSS, the term "box model" is used when referring to layout.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Variables and Random Numbers Computer App Session 4.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Action Script 12 "The games of a people.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Intro to Action Script 12 "The games of.
School of Computing and Information Systems RIA Animation, part I.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics.
Tank Game Part 2 of 6. Firing Shells Coming up… Players Scores Large Explosions Small Explosions Damage Health Bars Parent Shell Destructible Walls Reappear.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Lesson Seven: Using Flags. What Are Flags? Flags are Variables or Switches Used to Help With Logic Control of your character. Normally, Flags are used.
CHAPTER 15 Floating and Positioning. FLOAT VS. POSITION  Floating an element moves it to the left or right, allowing the following text to wrap around.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Martin Norris Computing Teacher/Leader at Moldgreen Community Primary School, Huddersfield
Web Design (17) CSS Box Element (2). ‘Box’ Web Site (1) Insert a navigation bar into the index.html file and create links to ‘index.html’, ‘pageone.html’
JQuery Element & Attribute Selectors, Events, HTML Manipulation, & CSS Manipulation.
Sound and more Animations
What is a Function Expression?
MOM! Phineas and Ferb are … Aims:
p5.js mouse, keyboard and if’s
Intro & Point-to-Box, Circle-to-Circle, Point-to-Circle
Advanced CSS BIS1523 – Lecture 20.
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Go to =>
Lecture 11: Keyboard Events
Web Programming and Design
Events Part III The event object.
Presentation transcript:

Set 7: Events and Animation IT452 Advanced Web and Internet Systems

Animation and Events Animation – attention, explanation Capturing user events Education and games We will use jQuery to make coding easier

Preliminaries /* Margin and padding on body element can introduce errors in determining element position and are not recommended; we turn them off. */ body { margin:0; padding:0 } #foo { width:50px; height:50px; background-color:#00f }

falling.html – An event loop var blockPos = [200,200]; // (x,y) coordinates of the block // This function is periodicaly called by the timer. It makes things move. function mainLoop() { moveBlock(); } // Do necessary setup on first load function init() { setInterval("mainLoop()", 25); // Call main loop every 50 milliseconds $(document).click(moveOnClick); // On every click, call the move() function } function moveBlock() { blockPos[0] += 1; // Update the block's current position. blockPos[1] += 2; // Move the block to its new position. $('#foo').offset({ left: blockPos[0], top: blockPos[1] }); } function moveOnClick(e) { // Get the location of the user's mouse click. blockPos[0] = e.pageX; blockPos[1] = e.pageY; // Move the block to the click point $('#foo').offset({ left: blockPos[0], top: blockPos[1] }); } $(document).ready(init); // When the document loads, startup the code!

falling.html (HTML portion) Using an event loop <div id=“bigdiv” style="width:600px; height:400px; background-color:blue">

jQuery Event Listeners 1.Use a selector to choose the object 2.Decide which event type to use 1.Click 2.Mouseover 3.Blur 4.Keypress 5.Etc. 3.Add a function reference or anonymous function How do we add a key listener to the div?

bounce.html (1) var bigdivPos = [0,100]; var bigdivSize = [600,400] function moveBlock() { // Get the block's current position. blockPos[0] += blockVeloc[0]; blockPos[1] += blockVeloc[1]; // Move the block to its new position. $('#foo').offset({ left: blockPos[0], top: blockPos[1] }); keepItemInsideBox(bigdivPos, bigdivSize, blockPos, blockSize, blockVeloc); } function init() { // Save the main container's rendered position on the window. var offset = $('#bigdiv').offset(); bigdivPos[0] = offset.left; bigdivPos[1] = offset.top; // Call main loop every 25 milliseconds setInterval("mainLoop()", 25); // On every click, call the move() function $(document).click(moveOnClick); }

bounce.html (2) // Checks to see if the block has hit the edge of the bigdiv. If so, bounce off // If we get outside the box, this will also force us to move back in. function keepItemInsideBox(boxPos, boxSize, itemPos, itemSize, itemVeloc) { // Check to see if we hit a vertical edge var edgeHit = calcVerticalEdgeHit(boxPos, boxSize, itemPos, itemSize); if ( (edgeHit == HIT_LEFT) && (itemVeloc[0] < 0) ) { // if too far left, make sure we go right itemVeloc[0] *= -1; } if ( (edgeHit == HIT_RIGHT) && (itemVeloc[0] > 0) ) { // if too far right, make sure we go left itemVeloc[0] *= -1; } // Check to see if we hit a vertical edge var edgeHit = calcHorizontalEdgeHit(bigdivPos, bigdivSize, blockPos, blockSize); if ( (edgeHit == HIT_TOP) && (itemVeloc[1] < 0) ) { // if too far up, make sure we go down itemVeloc[1] *= -1; } if ( (edgeHit == HIT_BOTTOM) && (itemVeloc[1] > 0) ) { // if too far down, make sure we go up itemVeloc[1] *= -1; } }

bounce.html (3) // See if the item hit a vertical edge by going too far left or right. // NOTE: we assume it was basically inside the box to begin with function calcVerticalEdgeHit(boxPos, boxSize, itemPos, itemSize) { // check for hit on left side var boxLeft = boxPos[0]; var boxRight = boxPos[0] + boxSize[0]; var itemLeft = itemPos[0]; var itemRight = itemPos[0] + itemSize[0]; if (itemLeft < boxLeft) return HIT_LEFT; else if (itemRight > boxRight) return HIT_RIGHT; } // See if the item hit a horizontal edge by going too far up or down. // NOTE: we assume it was basically inside the box to begin with function calcHorizontalEdgeHit(boxPos, boxSize, itemPos, itemSize) { // check for hit on left side var boxTop = boxPos[1]; var boxBot = boxPos[1] + boxSize[1]; var itemTop = itemPos[1]; var itemBot = itemPos[1] + itemSize[1]; if (itemTop < boxTop) return HIT_TOP; else if (itemBot > boxBot) return HIT_BOTTOM; }

Multiblocks.html // (x,y) coordinates of the block var block1Pos = [200,200]; var block1Size = [10,10] var block1Veloc = [1, 2]; var block2Pos = [300,300]; var block2Size = [10,10] var block2Veloc = [2, -1]; function mainLoop() { moveBlock('block1', block1Pos, block1Size, block1Veloc); moveBlock('block2', block2Pos, block2Size, block2Veloc); } // Move the block by a little bit function moveBlock(name, pos, size, veloc) { pos[0] = pos[0] + veloc[0]; pos[1] = pos[1] + veloc[1]; // Move the block to its new position. $("#"+name).offset({ left: pos[0], top: pos[1] }); keepItemInsideBox(bigdivPos, bigdivSize, pos, size, veloc); } … Using an event loop

Exercise 1.How would you make the blocks twice as FAST? 2.How else can the blocks move twice as FAST? 3.How would you make the blocks twice as BIG?

Game.html (1) // missile launcher var launcherPos = [150,500]; var launcherSize = [ 20, 20] // a missile. Put off board initially var offBoardPos = [800, 0]; var missilePos = offBoardPos; var missileSize = [40, 40]; var missileVeloc = null; // Do necessary setup on first load function init() { // Save the main container's rendered position on the window. var offset = $('#bigdiv').offset(); bigdivPos[0] = offset.left; bigdivPos[1] = offset.top; // Call main loop every 25 milliseconds setInterval("mainLoop()", 25); // On every click, call the fire function $(document).click(fireAtClick); // Create the blocks createDiv(block1Pos, block1Size, "myblock", "block1"); createDiv(block2Pos, block2Size, "myblock", "block2"); // Create the missile launcher createDiv(launcherPos, launcherSize, "launcher", "launcher"); // missile initially invisible createDiv (missilePos, missileSize, "invisible", "missile1"); }

Game.html (2) // Create a div with the given pos, size, and class function createDiv(pos, size, myclass, myid) { // create basic div var newobj = $(" "); $('body').append(newobj); newobj.addClass(myclass); newobj.css("width",size[0]+"px").css("height",size[1]+"px"); // move it to right place newobj.offset({ left: pos[0], top: pos[1] }); } // Update the score! function incrementScore(val) { score = score + val; $('#scorebox').text("Score: "+score); }

Game.html(3) // Check for a missile hit. If hit, act like block destroyed, get rid of missile function checkMissileHit(missileId, missilePos, missileSize, blockPos, blockSize) { if (!didSpheresHit(missilePos, missileSize, blockPos, blockSize)) return; // we did have a hit. Make the missile invisible and immobile $('#missile1').addClass("invisible"); $('#missile1').removeClass("missile"); missileVeloc = null; missilePos = offBoardPos; $('#'+missileId).offset({ left: missilePos[0], top: missilePos[1] }); incrementScore(100); // Randomize location of new block (somewhere inside bigdiv) blockPos[0] = bigdivPos[0] + Math.random() * bigdivSize[0]; blockPos[1] = bigdivPos[1] + Math.random() * bigdivSize[1]; }

Game.html(3) // Check if the spheres with given pos and sizes are touching. // Assumes width equal to height function didSpheresHit(pos1, size1, pos2, size2) { // We have positions of top left corners. Convert to center positions var cx1 = pos1[0] + size1[0] / 2; var cx2 = pos2[0] + size2[0] / 2; var cy1 = pos1[1] + size1[1] / 2; var cy2 = pos2[1] + size2[1] / 2; // find distance between centers var dx = cx1 - cx2; var dy = cy1 - cy2; dist = Math.sqrt(dx*dx + dy*dy); // size tracks the width, so radius would be half of that. // So they touch if dist less than (width1 + width2) / 2 if (dist < ( (size1[0] + size2[0] ) / 2) ) return true; else return false; }

Game.html (4) // When we click, fire a missile at that location function fireAtClick(e) { // calculate slope toward that spot var dx = e.pageX - launcherPos[0]; var dy = e.pageY - launcherPos[1]; // scale down by the overall distance. not necc. the right math, but // gets it moving in the right direction var dist = Math.sqrt(dx*dx + dy*dy); dx = 2 * dx / dist; dy = 2 * dy / dist; // Make coordinates for the missile missileVeloc = [ dx, dy ]; // get COPY of that array, not the same one missilePos = [ launcherPos[0], launcherPos[1] ]; // Make missile visible. $('#missile1').addClass("missile"); $('#missile1').removeClass("invisible"); }

Further Example game-keypress.html – add keyboard movement (NOTE: array keys more complex, ask if needed)

JavaScript Objects blocks = []; // an empty array // Create the first block blocks[0] = new Object(); blocks[0].pos = [200, 200]; blocks[0].size = [10, 10]; blocks[0].veloc = [1, 2]; blocks[0].elem = createDiv(blocks[0].pos, blocks[0].size, "class_myblock"); // Create the second block blocks[1] = new Object(); blocks[1].pos = [300, 300]; blocks[1].size = [10, 10]; blocks[1].veloc = [1, 2]; blocks[1].elem = createDiv(blocks[1].pos, blocks[1].size, "class_myblock");

JS Objects: alternative syntax // Create the game board ("bigdiv") bigdiv = new Object(); bigdiv.pos = [100, 100]; bigdiv.size = [500, 200]; bigdiv.elem = createDiv(bigdiv.pos, bigdiv.size, "class_bigdiv"); // Create the game board ("bigdiv") bigdiv = { pos: [100, 100], size: [500, 200] }; bigdiv.elem = createDiv(bigdiv.pos, bigdiv.size, "class_bigdiv") OR

Game Design Think about the key elements –Mechanics –Story –Aesthetics –Technology JS/jQuery speed may be an issue –Test early and often! –Design and test Start thinking about your game!