Baklava - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Baklava

Description:

Source code and documentation are included. There are quite a lot of methods, but it's easier to learn ... Put the folder as a subfolder of your working folder ... – PowerPoint PPT presentation

Number of Views:755
Avg rating:3.0/5.0
Slides: 23
Provided by: villa81
Category:

less

Transcript and Presenter's Notes

Title: Baklava


1
Baklava
2
Baklava
  • Baklava is a free Sprite Animation Toolkit
  • Source code and documentation are included
  • There are quite a lot of methods, but it's easier
    to learn them than to write them all yourself
  • Baklava uses Java 1.0
  • Baklava appears to support only Applets
  • http//www.boutell.com/baklava/

3
Baklava concepts
  • Baklava has Sprites and a Playfield
  • Any visible object that has a behavior (moves,
    interacts with other objects) should be a Sprite
  • Baklava Sprites have useful predefined behaviors
  • holding and displaying an image
  • movement, and collisions
  • detecting mouse clicks, etc.
  • ...but you still probably need to extend the
    Sprite class
  • The Playfield is just a container for Sprites

4
Backgrounds
  • A background is just an image
  • Backgrounds typically have no behavior
  • Even a moving background doesn't do anything
  • In side-scrolling games with ledges, etc., the
    ledges are Sprites the rest may or may not be
  • In Baklava, a background is a kind of Sprite

5
Importing Baklava
  • Download the Baklava classes
  • There are only five Sprite, Playfield, Timer,
    GlobalTimerObserver, and FastVector
  • Make sure these are in a folder named baklava
  • Put the folder as a subfolder of your working
    folder
  • If you put the baklava folder somewhere else, add
    it to your CLASSPATH
  • Add import baklava. to your program

6
Directory structure
  • myWorkingDirectory
  • myProgram.java
  • myProgram.class
  • myHero.gif
  • myMonster.gif
  • baklava (folder)
  • Sprite.class
  • Playfield.class
  • etc.

7
How to use Baklava
  • import baklava.
  • Create a Playfield
  • Create some Sprites
  • Sprites must be created in a Playfield
  • Tell the Sprites what to do
  • There are many properties you can set
  • For additional behaviors, extend the Sprite class
  • Start and display the Playfield

8
Create a Playfield
  • Playfield p
  • p new Playfield(this,
    getBounds().width,
    getBounds().height)
  • this the Applet itself
  • getBounds() returns the size of the Applet
  • For simple Applets, use setLayout(null)

9
Create some Sprites
  • Sprite s new Sprite(p)
  • p the Playground
  • Sprite ss new SpritenSpritesfor (int i
    0 i lt nSprites i) ssi new Sprite(p)
  • Creating a background Sprite
  • Sprite background new Sprite(p)background.setB
    ackground(true)

10
Attaching images to Sprites
  • First, find out where the Applet is
  • base getCodeBase().toString()
  • Next, load the image from the same place
  • Image image p.getImage(base "/image.jpg")
  • Finally, attach the image to the Sprite
  • s.setImage(image)
  • You can also
  • Load from an images subdirectory
  • Combine the loading and attaching steps

11
Telling Sprites how to move
  • sprite.setDirection(degrees)
  • sprite.setSpeed(pixelsPerSecond)

12
Sprite behavior at edges
  • sprite.setEdgeHandling(action)
  • tells the Sprite what to do when it hits the edge
  • action is one of
  • Sprite.edgeSolid -- just stop moving
  • Sprite.edgeBounce -- bounce off edge
  • Sprite.edgeWrap -- go off and reappear on other
    side

13
Telling Sprites where to go
  • sprite.setDirectionToward(int x, int y)
  • sprite.setDirectionToward(anotherSprite)
  • sprite.setTarget(int x, int y)
  • Sprite automatically stops moving when it
    arrives, and calls the next method
  • sprite.onArrival( )
  • This method does nothing (override it)

14
Z-order
  • Sprites have a Z position as well as X and Y
  • Larger Z values are "in front," closer to the
    user
  • sprite.setLevel(int level)
  • Sprites are created at level 0
  • A background should be set to level -1
  • Sprites at different levels can still collide

15
Collisions
  • Baklava detects collisions between Sprites...
  • ...but it doesn't do anything about collisions
  • To do something when there is a collision
  • Create a subclass of Sprite
  • Override public void collisionWith(Sprite other)
  • Each Sprite will get a collisionWith message
  • If they interpenetrate, they will get multiple
    messages

16
What did I just hit?
  • Java has a class named Class
  • object.getClass( ) returns the Class to which the
    object belongs
  • Class.forName(String className) returns the Class
    with the given name
  • Class objects can be compared with
  • Example
  • public void collisionWith(Sprite other) if
    (other.getClass() this.getClass()) ...

17
Using a timer
  • You can set a "timer" on a Sprite
  • sprite.setTimer(int milliseconds, int id)
  • To react to the timer, override timer(int id)
  • public void timer(int id) switch(id) ....
  • You can set a timer that notifies every Sprite
  • public void setTimerAll(int delay, int timerId)
  • There are also "global" timers to send timer
    events to the Applet itself

18
Responding to keypresses
  • Keyboard and mouse handling is Java 1.0
  • I hope to be able to do something about that
  • Meanwhile,
  • public void keyDown(Event evt, int key)
    if (((char) key) ' ') ...
  • Every Sprite will receive this message

19
Responding to mouse clicks
  • void mouseDown(Event evt, int x, int y)
  • void mouseUp(Event evt, int x, int y)
  • void mouseMove(Event evt, int x, int y)
  • void mouseDrag(Event evt, int x, int y)
  • void mouseEnter(Event evt, int x, int y)
  • void mouseExit(Event evt, int x, int y)
  • Playfield.mouseDrag( ) erroneously calls
    Sprite.mouseMove( ) instead of Sprite.mouseDrag( )

20
Dragging a Sprite
  • To drag a Sprite
  • On mouseDown( ), save old speed and set speed to
    0
  • For each mouseMove( ) event, adjust Sprite x and
    y
  • On mouseUp( ), restore speed (if desired)
  • Adjusting the Sprite's x and y
  • public void mouseMove(Event event, int x, int
    y) if (beingDragged)
    setX(getX() x - oldMouseX)
    setY(getY() y - oldMouseY)

21
Problems with dragging
  • The Java 1.0 event model isn't very good
  • Playfield.mouseDrag( ) erroneously calls
    Sprite.mouseMove( ) instead of Sprite.mouseDrag(
    )
  • If you move too fast you can "drop" the Sprite
  • These problems can be fixed, with some effort
  • Baklava still seems a good starting point

22
The End
Write a Comment
User Comments (0)
About PowerShow.com