Roy McElmurry. More:

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Create a Simple Game in Scratch
AS3: Events & Listeners eventListeners in ActionScript 3.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Cosc 5/4730 Input Keyboard, touch, and Accelerometer.
Chapter 5 Multitable Forms
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Roy McElmurry. ActionScript : an object-oriented programming language similar to JavaScript MXML : A flavor of XML that helps simplify user interface.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
1 Flash Actionscript Event Handling. 2 Event Handling Right now we know all about variables lets go back to our text input/output example: Suppose we.
1 Flash Actionscript Animation. 2 Introduction to Sprites We will now look at implementing Sprites in Flash. We should know enough after this to create.
1 Flash Actionscript Actionscript and Objects. 2 Actionscript and Objects ActionScript is what's known as an object-oriented programming language. Object-oriented.
SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2010SCM-CityU1.
Events (Listeners/Handlers: Mouse, keyboard, timer) Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer
Games and Simulations O-O Programming in Java The Walker School
© 2010 Delmar, Cengage Learning Chapter 6: Preparing and Publishing Movies.
GAME:IT Junior Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Guide to Programming with Python
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Game Design Creating a game called PING Phase 3: Steps for building basic game.
GAME:IT Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
1. Chapter 18 Managing Lists 3 Working with Lists and Symbols Set off important information by formatting it as a bulleted or numbered list. Customize.
© 2011 Delmar, Cengage Learning Chapter 9 Introduction to ActionScript 3.0.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
© 2010 Delmar, Cengage Learning Chapter 3: Working with Symbols and Interactivity.
Getting a handle on ActionScript A basic primer for non-programmers.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Using Client-Side Scripts to Enhance Web Applications 1.
AD 305 Electronic Visualization I : School of Art and Design : University of Illinois at Chicago : Spring 2007 Intro to Action Script 2 "The games of a.
Lab 6: event & input intro User Interface Lab: GUI Lab Oct. 2 nd, 2013.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
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.
ActionScript: Loading External Content with MovieClipLoader Class, Listener Objects, Preloaders, HitTest, Loading Sound MMP 220 Multimedia Programming.
Game Maker Terminology
Programming games Show shaking screen, quiz show. Calculations Homework: (Finish basic Javascript Projects) Make proposal. Work on project.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
© 2011 Delmar, Cengage Learning Chapter 10 Using ActionScript to Enhance User Experience.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
Computer Game Design ActionScript is… Object-oriented programming Everything you do in ActionScript does something to some object* Some objects.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
Video in Macromedia Flash (Optional) – Lesson 121 Video in Macromedia Flash (Optional) Lesson 12.
Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
CISC 110 Day 7 “The Outliers, Part1” hitTest(), Text Input, Frame and Timer Loops, Publishing Flash Content.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Flash Session 3: Events Shiny Yang special thanks to Alex
Flash Session 4: Importing Assets
Sound and more Animations
Collision Theory and Logic
var variableName:datatype;
MOM! Phineas and Ferb are … Aims:
Scratch for Interactivity
Collision Theory and Logic
Keyboard Input.
Introduction to JavaScript Events
Events in Action script
Actionscript Session 2 Roy McElmurry.
More programming with "Processing"
Week 6: Time and triggers!
Presentation transcript:

Roy McElmurry

More:  The TextFormat object can be used to set many other properties as well. Syntax: import flash.text.*; var textField :TextField = new TextField(); … var format :TextFormat = new TextFormat( font, size, color, …); textField.setTextFormat( format );

 The auto size property must be set so that the TextField knows how it should respond to formatting  In this case in treats the text as left justified Syntax: import flash.text.*; var textField :TextField = new TextField(); … var format :TextFormat = new TextFormat( font, size, color, …); textField.autoSize = TextFieldAutoSize.LEFT; textField.setTextFormat( format ); More:

More: ConstantsConstants  The for-each loop iterates over the indices of any iterable object, not the values  You can then use the index to access that data at that index Syntax: var stuff :Array = { thing1, thing2, thing3, …}; for (var key : type in stuff ) { //use key as index //get data with stuff [ key ] }

 FlashVars are a way for us to load a flash movie with parameters  The format is the same as that for HTML query strings  You can also just add a query string to the end of the data attribute Syntax: <object type=“application/x-shockwave-flash” data=“ yourfile.swf ” width=“ 550 ” height=“ 400 ” >

 You can treat the params object as if it were an associative Array Syntax: import flash.display.LoaderInfo; … var params :Object = LoaderInfo(this.root.loaderInfo).parameters; for (var key :String in params ) { //do stuff with the keys of the FlashVars //value = params[key] }

 Events are flags that are raised by certain objects for different purposes  In many cases we want something to happen when this event occurs, some such events may include  A button being clicked  The user pressing a key on the keyboard  A picture having finished loading  A timer going off  When we use addEventListener we are saying that we wish for the listener function to be executed when the given type of event occurs

 The name of the listener function must exactly match the function name given to addEventListener()  The listener function must take a single parameter of the same type as that specified when you used addEventListener()  The parameter contains valuable information about the event that one might want to use Syntax: public function listener (ev: Type ):void { //do stuff }

 The Event parameter stores valuable data  target and currentTarget can differ when the same event is being listened for by multiple things Syntax: public function listener (ev: Type ):void { //ev can be very useful } Fields ev.targetThe object that started the event. ev.typeContains metadata about the sound such as artist, duration, title, etc. ev.currentTargetThe object that is currently responding to the event More:

Syntax: import flash.display.Loader; import flash.display.Bitmap; … var loader :Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded ); loader.load(“ myimage.jpg ”); … public function loaded ( e :Event):void { var img :Bitmap = new Bitmap( e.target.content.bitmapData); addChild( img ); }

 Using the embed command is a shortcut for loading a resource and responding to it’s load complete event  This command must be placed in the fields area of your class  In practice it would be better style to initialize name2 in the constructor Syntax: [Embed(source=“/ filename.ext ”)] private var name1 :Class; private var name2 : Type = new name1() as Type ;

Syntax: import flash.media.Sound; … [Embed(source=“/ filename.mp3 ”)] private var name1 :Class; private var name2 :Sound = new name1() as Sound; Functions/Fields sound.play();Plays the mp3, returns a SoundChannel object which could be used to tinker with the sound sound.id3Contains metadata about the sound such as artist, duration, title, etc. sound.lengthReturns the length of the mp3 More:

Syntax: import flash.display.Bitmap; … [Embed(source=“/ filename.jpg ”)]//also png or gif private var name1 :Class; private var name2 :Bitmap = new name1() as Bitmap; Functions pic.getBounds(obj:DisplayObject)Returns a Rectangle object that defines the area of pic relative to obj pic.hitTestObject(obj:DisplayObject);Determines if pic is intersecting the given DisplayObject pic.hitTestPoint(x:Number, y:Number);Determines if pic intersects (x, y) More:

 In all games it is necessary to have a game loop  A game loop is some logic that is performed every so often to maintain the game environment  Things that need to happen regularly should be in the game loop  Ex)  Moving things that are not user-controlled  Detecting collisions  A game loop for pong might only move the ball

 We want our ball to move in a random direction  One approach is to store the direction of the ball in a Point object  The x and y fields of the Point will represent the direction that the ball will move in  When using a direction vector the Point must be normalized to be length one, otherwise speed will vary

Syntax: import flash.geom.Point; … var name :Point = new Point(x:Number, y:Number); Functions Point.distance(p1:Point, p2:Point);Returns the distance between the two points Point.polar(len:Number, angle:Number); Returns a Point with cartesian coordinates, where angle is in radians p1.add(p2:Point);Add the coordinates of p2 to p1 p1.offset(dx:Number, dy:Number);Offset p1 by dx and dy More:

 In games it is often important to know when objects on the screen are touching  Even the simplest games need to respond to objects colliding with either other objects or specific points  Examples  Pong – ball bounce  Worm – eating an apple  Galaga – bullets hitting enemies  Halo – pwning newbs

 If the rectangle that surrounds s1 intersects the rectangle that contains s2, then hitTest will return true, otherwise it returns false  This can be called on any DisplayObject Syntax: var s1:Shape; var s2:Shape; … s1.hitTestObject(s2); More:

 If the rectangle that surrounds s1 overlaps or intersects the point specified  You can supply a third optional boolean parameter for whether to check if the actual pixels of the object are touching instead of just the bounding box  This can called on any DisplayObject Syntax: var s1:Shape; … s1.hitTestPoint( x, y ); More:

 There is also a KEY_UP event that you can use Syntax: import flash.events.*; … stage.addEventListener(KeyboardEvent.KEY_DOWN, listener ); … public function listener ( ev :KeyboardEvent):void { //respond to user action }

 KeyboardEvent s have tons of useful information in them Fields ev.charCodeThe ASCII value of the character that was pressed, all modifier keys have a keyCode of 0 ev.keyCodeA numeric code for the which button on the keyboard was pressed ev.keyLocationWhere the key is that was pressed ev.shiftKeyBoolean for whether the shift key was being pressed ev.altKeyBoolean for whether the alt key was being pressed ev.ctrlKeyBoolean for whether the control key was being pressed More:

keykeyCodecharCode w11987 a9765 s11583 d10068 space32 up038 left037 right039 down040  The Keyboard class contains constants for a lot of these so that we do not have to memorize the numbers More: More:

 The KEY_DOWN event is fired once and then after a pause is fired regularly as we would expect, but the delay creates for a bad user experience  Pressing a different key will overshadow a previous key that is still being pressed meaning that holding down two keys will only register one KEY_DOWN event  We can fix this issue by using the KEY_DOWN and KEY_UP events  The idea is to key track of which keys are down and then update in the game loop

Syntax: import flash.events.*; … var keysPressed :Array = []; stage.addEventListener(KeyboardEvent.KEY_DOWN, downListener ); stage.addEventListener(KeyboardEvent.KEY_UP, upListener ); … public function downListener ( ev :KeyboardEvent):void { keysPressed.push(ev.charCode); } public function upListener ( ev :KeyboardEvent):void { removeKey ( keysPressed, ev.charCode); } public function gameLoop ():void { for (var key: int in keysPressed ) { //respond to key being down } //You need to write a removeKey function as well

 Game programming can get complex and there can be lots of subtle bugs associated with it, like the KEY_DOWN event issues  There are many libraries that help us with Flash game programming  Ex)  Flixel with the FlashDevelop IDE  FlashPunk  APE (Actionscript Physics Engine)