LCC 6310 Computation as an Expressive Medium - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

LCC 6310 Computation as an Expressive Medium

Description:

Like asteroids and rockets, the missile class should know how to draw itself. A Missile is similar to a rocket (position, rotation, draw method, etc. ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 11
Provided by: michael74
Category:

less

Transcript and Presenter's Notes

Title: LCC 6310 Computation as an Expressive Medium


1
LCC 6310Computation as an Expressive Medium
  • Lecture 5

2
Outline
  • Programming concepts
  • Subclasses
  • Methods that return values
  • BImage
  • Discuss Stallman article
  • Talk about Assignment 2

3
Revisit our example
  • So far we have a rocket that flies around in a
    field of asteroids
  • What if we want our rocket to be able to fire
  • But we dont want to get rid of our non-firing
    rocket
  • Create a subclass!

4
Inheritance
  • Subclasses inherit fields and methods from parent
  • Class ArmedRocket extends Rocket

5
Our subclass needs a constructor
  • Our empty ArmedRocket example creates an error
  • Processing doesnt know how to construct an
    ArmedRocket
  • We want the ArmedRocket constructor to do the
    same work as the Rocket constructor
  • ArmedRocket(int initialX, int initialY, float
    initialRot)
  • super(initialX, initialY, initialRot)
  • The keyword super means to call the method in the
    parent with the same name

6
Now we have ArmedRocket
  • We can use an ArmedRocket now in our example
  • But, its basically just a copy of Rocket
  • The only reason to define an ArmedRocket is to
    add new capabilities or to override old ones

7
Add a fire() method
  • We want our fire method to draw a missile that
    shoots out of the rocket
  • We could have the fire method draw the missile
  • Is there a problem with this?

8
Missiles should also be objects
  • The object oriented solution is to make the
    missile an object as well
  • All the different types of things in our domain
    should have a corresponding class
  • Like asteroids and rockets, the missile class
    should know how to draw itself
  • A Missile is similar to a rocket (position,
    rotation, draw method, etc.)
  • Now our ArmedRocket.fire() method can just create
    and return a missile

9
The fire() method
  • Missile fire()
  • Missile m new Missile(xPos, yPos,
    rotation)
  • return m
  • Now add code in loop to draw missiles

10
Using the keyPressed() method
  • keyPressed() is a built in Processing method that
    is called when a key is pressed
  • Useful because you can tie events to keystrokes
    instead of to loop()
  • If we fire missiles in loop(), notice that we
    fire many times even if we push the key quickly
  • This is because loop() is called many times a
    second
Write a Comment
User Comments (0)
About PowerShow.com