Enhancing JavaScript Using Application Programming Interfaces (APIs)

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

By Waqas Over the many years the people have studied software-development approaches to figure out which approaches are quickest, cheapest, most.
When is Orientated Programming NOT? Mike Fitzpatrick.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Object Oriented System Development with VB .NET
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Information Technology Center Hany Abdelwahab Computer Specialist.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 13: Object-Oriented Programming
C++ fundamentals.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Introducing Object-Oriented Programming (OOP)
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
 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.
CSCI N341: Client-Side Web Programming Copyright ©2004  Department of Computer & Information Science "Object-Oriented" JavaScript.
Object Oriented Software Development
A First Program Using C#
Introduction to Object-oriented programming and software development Lecture 1.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
An Object-Oriented Approach to Programming Logic and Design
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
1 Pertemuan 26 Bahasa-bahasa Pemrograman (Lanjutan) Matakuliah: T0604-Pengantar Teknologi Informasi Tahun: 2008 Versi: 2.0/0.0 Williams, B.K, Stacy C.
JavaScript, Fourth Edition
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Programming games Flash drawing trick(s). Past due: Your completion of rock-paper-scissors) Classwork: Bouncing ball: read tutorial. Start coding.
Programming games Show your version of Bo the dog. Start cannonball Preview: video, audio work session (cannonball) Homework: Cannonball with ball in a.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Principles of Software Development 1 Principles Of Software Design and Development Types of language / Choosing a language.
Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS324e - Elements of Graphics and Visualization Timing Framework.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
© 2011 Delmar, Cengage Learning Chapter 10 Using ActionScript to Enhance User Experience.
Introduction into JavaScript Java 1 JavaScript JavaScript programs run from within an HTML document The statements that make up a program in an HTML.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Programming Paradigms Different paradigms Procedural paradigm, e.g. Pascal Basic Functional paradigm, e.g. Lisp Declarative paradigm, e.g. Prolog Object-Oriented.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Web Programming Overview. Introduction HTML is limited - it cannot manipulate data How Web pages are extended (include): –Java: an object-oriented programming.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.
Copyright ©2005  Department of Computer & Information Science Object Based JavaScript.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
 By the end of this lecture, you should …  Understand the three pillars of Object- Oriented Programming: Inheritance, Encapsulation and Polymorphism.
1 Object-Oriented Programming Using C++ CLASS 17 Honors.
Enhancing JavaScript Using Application Programming Interfaces (APIs), Lecture #3.
Object-Orientated Programming
Object Oriented Programming
JAVA By Waqas.
Week 4 Object-Oriented Programming (1): Inheritance
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
MSIS 670 Object-Oriented Software Engineering
Java Applets.
Programming games Demonstrate cannonball
Object-Oriented PHP (1)
Presentation transcript:

Enhancing JavaScript Using Application Programming Interfaces (APIs)

Goals By the end of this unit you should understand … How to create re-usable objects Why we use APIs How to create & use sprites How to use sprite methods How to animate using sprites

OOP Design Principles To address the industry needs of cost overruns and delays, the OOP model was built upon 3 key principles: inheritance, encapsulation, and polymorphism Although inheritance and encapsulation are supported, at least in part, by JavaScript, polymorphism is not supported.

Inheritance Inheritance says this: the developers of an OOP language create a library of known objects, organized into groups called packages and classes These pre-defined objects already know how to perform many methods, and come with a framework for accompanying attributes For example, in Java there is a button class, which has an attribute of a label, and can perform certain tasks when clicked

Inheritance What this means is that writing code from scratch should be minimized You start with the existing libraries, and just add only what you need Specifically, you can start with a specific object, and modify it ever so slightly to get an added benefit The principle of inheritance says that if you create an object as a “child” of another object, the new child INHERITS all the properties and knowledge of its parent… you only add the custom content So, you get a lot of your programming solution for free, by artfully using inheritance to take advantage of existing code

Encapsulation Encapsulation, also referred to as data hiding, traces its history to subroutines and functions It says this: I will create code, and place a software fence around it, to create a divide and conquer effect If one object passes a message to another, asking that a method be performed, the calling object doesn’t need to know the gory details of how the task was performed… it simply needs a final result back from the called object The called object is using encapsulation… its method detail and attribute values can remain hidden from view – and thereby protected.

Encapsulation Encapsulation adds some built in quality control to programming… It accomplishes this by restricting access to variables and blocks of code, and by forcing programmers to think in terms of small, focused subsections of code

Polymorphism – Not Supported By JavaScript The principle of polymorphism, like inheritance, addressed the marketing needs of creating reusable code portable to other applications It says this – I have already created an object, or written a method, or created a class that performs a similar tasks…with slight modifications, I can use it here

Polymorphism – Not Supported By JavaScript A polymorphic design is one written in general enough terms that it supports application in other ways For example, imagine writing a close method for a door object. If you were thoughtful enough in the design, the same method could be utilized by a window object, or maybe even a mouth object!

“Object-Oriented” JavaScript More like “Object-Inspired” JavaScript We can create new, custom, re- usable objects in JavaScript that include their own methods, properties and events. Consider the following problem: “I want to record the color, brand, horsepower and price of several cars”

Solution without Objects Use parallel arrays Can be confusing Difficult to keep track of which car has which color, brand, etc. Example:

Solution with Objects Calls to an API that contains the custom car object Much cleaner code Re-usable object

What is an API? API stands for Application Programming Interface Allows programmers to extend the current language to included customized components Most modern languages incorporate APIs For our demonstrations, we’ll use an API known as GameLib

Downloading & Installing GameLib Click the following link to download GameLib: Create a sibling directory of your “n341” directory called “gameLib” Copy all *.js files from the GameLib library to your gameLib directory Example of program using GameLib:

Game Programming: Sprites Sprites are graphic objects used in game programming Sprites can react to user events by changing location, changing shape, size, etc. JavaScript does not support sprites directly; however, we can create an object that works and acts like a sprite via GameLib (or write our own!)

Using GameLib to Create a Sprite Examine the following script: Notice the way we import the GameLib libraries in the script: </script>

Creating the Sprite Object var ball; function init(){ ball = new Sp_Sprite(); ball = new Sp_Sprite(); ball.setImage("redball.gif", 20, 20, 1, 1); ball.setImage("redball.gif", 20, 20, 1, 1); ball.moveTo(100,100); ball.moveTo(100,100); ball.setXlimits(0, 500); ball.setXlimits(0, 500); ball.setYlimits(0, 300); ball.setYlimits(0, 300); ball.setFrame(0); ball.setFrame(0); ball.switchOn(); ball.switchOn(); } // end init </script> What’s Going On? In this line, we create a variable called “ball.” We’ll specify exactly what type of object ball is later. What’s Going On? In this line, we create a variable called “ball.” We’ll specify exactly what type of object ball is later.

Creating the Sprite Object var ball; function init(){ ball = new Sp_Sprite(); ball = new Sp_Sprite(); ball.setImage("redball.gif", 20, 20, 1, 1); ball.setImage("redball.gif", 20, 20, 1, 1); ball.moveTo(100,100); ball.moveTo(100,100); ball.setXlimits(0, 500); ball.setXlimits(0, 500); ball.setYlimits(0, 300); ball.setYlimits(0, 300); ball.setFrame(0); ball.setFrame(0); ball.switchOn(); ball.switchOn(); } // end init </script> What’s Going On? In this line, we create a function called init() that will initialize the ball variable as a sprite object. init() will be called later by the onLoad event in the tag. What’s Going On? In this line, we create a function called init() that will initialize the ball variable as a sprite object. init() will be called later by the onLoad event in the tag.

Creating the Sprite Object var ball; function init(){ ball = new Sp_Sprite(); ball = new Sp_Sprite(); ball.setImage("redball.gif", 20, 20, 1, 1); ball.setImage("redball.gif", 20, 20, 1, 1); ball.moveTo(100,100); ball.moveTo(100,100); ball.setXlimits(0, 500); ball.setXlimits(0, 500); ball.setYlimits(0, 300); ball.setYlimits(0, 300); ball.setFrame(0); ball.setFrame(0); ball.switchOn(); ball.switchOn(); } // end init </script> What’s Going On? In this line, we create “ball” as a sprite object using the constructor called “Sp_Sprite().” The constructor creates an instance of the sprite object and copies all methods & properties of the sprite to the variable ball. What’s Going On? In this line, we create “ball” as a sprite object using the constructor called “Sp_Sprite().” The constructor creates an instance of the sprite object and copies all methods & properties of the sprite to the variable ball.

Creating the Sprite Object var ball; function init(){ ball = new Sp_Sprite(); ball = new Sp_Sprite(); ball.setImage("redball.gif", 20, 20, 1, 1); ball.setImage("redball.gif", 20, 20, 1, 1); ball.moveTo(100,100); ball.moveTo(100,100); ball.setXlimits(0, 500); ball.setXlimits(0, 500); ball.setYlimits(0, 300); ball.setYlimits(0, 300); ball.setFrame(0); ball.setFrame(0); ball.switchOn(); ball.switchOn(); } // end init </script> What’s Going On? In this line, give some real dimension to our sprite. We specify the source of the object (“redball.gif”), the length and width (20, 20) and the frame and animation levels of the object (1, 1). We’ll learn about those last two later! What’s Going On? In this line, give some real dimension to our sprite. We specify the source of the object (“redball.gif”), the length and width (20, 20) and the frame and animation levels of the object (1, 1). We’ll learn about those last two later!

Creating the Sprite Object var ball; function init(){ ball = new Sp_Sprite(); ball = new Sp_Sprite(); ball.setImage("redball.gif", 20, 20, 1, 1); ball.setImage("redball.gif", 20, 20, 1, 1); ball.moveTo(100,100); ball.moveTo(100,100); ball.setXlimits(0, 500); ball.setXlimits(0, 500); ball.setYlimits(0, 300); ball.setYlimits(0, 300); ball.setFrame(0); ball.setFrame(0); ball.switchOn(); ball.switchOn(); } // end init </script> What’s Going On? In this line, we specify the initial location of the sprite on the page. Specifically, we specify the location of the upper-left corner of the sprite. What’s Going On? In this line, we specify the initial location of the sprite on the page. Specifically, we specify the location of the upper-left corner of the sprite.

Creating the Sprite Object var ball; function init(){ ball = new Sp_Sprite(); ball = new Sp_Sprite(); ball.setImage("redball.gif", 20, 20, 1, 1); ball.setImage("redball.gif", 20, 20, 1, 1); ball.moveTo(100,100); ball.moveTo(100,100); ball.setXlimits(0, 500); ball.setXlimits(0, 500); ball.setYlimits(0, 300); ball.setYlimits(0, 300); ball.setFrame(0); ball.setFrame(0); ball.switchOn(); ball.switchOn(); } // end init </script> What’s Going On? In these lines, we specify the horizontal boundary (setXlimits(…)) and vertical boundary (setYlimits(…)) of the sprite. It can’t be moved beyond these boundaries. What’s Going On? In these lines, we specify the horizontal boundary (setXlimits(…)) and vertical boundary (setYlimits(…)) of the sprite. It can’t be moved beyond these boundaries.

Creating the Sprite Object var ball; function init(){ ball = new Sp_Sprite(); ball = new Sp_Sprite(); ball.setImage("redball.gif", 20, 20, 1, 1); ball.setImage("redball.gif", 20, 20, 1, 1); ball.moveTo(100,100); ball.moveTo(100,100); ball.setXlimits(0, 500); ball.setXlimits(0, 500); ball.setYlimits(0, 300); ball.setYlimits(0, 300); ball.setFrame(0); ball.setFrame(0); ball.switchOn(); ball.switchOn(); } // end init </script> What’s Going On? These lines represent “housekeeping” methods. setFrame(0) sets the initial frame to zero for animation and switchOn() makes the sprite visible. What’s Going On? These lines represent “housekeeping” methods. setFrame(0) sets the initial frame to zero for animation and switchOn() makes the sprite visible.

Understanding Directions Using GameLib, to understand directions for sprite movement, we need to revisit the angles represented by a compass: N – 0º NE – 45º E – 90º SE – 135º S – 180º SW – 225º W – 270º NW – 315º

Moving the Sprite – init() Changes The first thing that we need to do is to modify our init() method to prepare for moving. ball.bounces = true; We add the following to modify the bounces property of the sprite object to enable “bouncing”: ball.bounces = true; G1_start(); Next, we add the following to init(): G1_start();

G1_start() G1_start() seems a simple method, but is very powerful. It begins the gameLib engine, which enables movement (and other functions). It also is responsible for staring a repeating timer that activates motion (the time repeats 20x/sec.)

Moving the Sprite - Example See the following for an example of moving the sprite object: See the next slide for explanations of the example …

Moving Sprite Example In the script, you see several buttons, created in HTML, that react to the onClick event:

Moving Sprite Example When onClick is activated, the moveBall() function is called and an integer-type parameter is sent to the function.

Moving the Sprite Example Looking at the moveBall() function, we can see that the value passed to the direc argument represents the angle at which the ball moves: function moveBall(direc){ if (direc == 999){ ball.setSpeed(0); } else{ ball.setSpeed(10); ball.setXYdegs(direc); } // end if } // end moveBall

Moving the Sprite Example function moveBall(direc){ if (direc == 999){ ball.setSpeed(0); } else{ ball.setSpeed(10); ball.setXYdegs(direc); } // end if } // end moveBall An argument of 999 passed to the function will use the sprite object’s setSpeed() method (from GameLib) to stop sprite movement.

Moving the Sprite Example function moveBall(direc){ if (direc == 999){ ball.setSpeed(0); } else{ ball.setSpeed(10); ball.setXYdegs(direc); } // end if } // end moveBall An argument other than 999 passed to the function will use the sprite object’s setSpeed() method to set sprite movement at 10 AND will use the argument to set the direction of sprite movement (according to compass angle) using the setXYdegs() method.

Next Time … Using frame animation in sprites Detecting collision of sprites Working with a timer

Questions?