Sunday, April 22, 2007

JAVA 認證心得 -- SCMAD (9)



  • Canvas

- protected void keyPressed(int keyCode)

- protected void keyReleased(int keyCode)

- protected void keyPepeated(int keyCode)

- int getGameAction(int keyCode)

- Boolean hasPointerEvents()

- int getKeyCode(int gameAction)

- protected abstract void paint(Graphic g)

  • Canvas class can have a Ticker, cannot hold item and show list.Only Form class or its sub-classes can hold Item objects.
  • GameCanvas

    - int getKeyStates()

    - void flushGraphics({int x,int y,int width,int height})

    Does’t return until the flush has been completed. The contents of the off-screen buffer remain unmodified after the flushing operation.

    - protected Graphics getGraphics()

    - void paint(Graphic g)

  • A dedicated buffer is created for each GameCanvas instance. The buffer is initially filled with white pixels. The developer can assume that the contents of this buffer are modified only by calls to the Graphics object(s) obtained from the GameCanvas instance; the contents are not modified by external sources such as other MIDlets or system-level notifications.
  • When the GameCanvas becomes invisible, getKeyStates() returns 0. The visibility of the GameCanvas can be verfied by calling Displayable.isShown(), which would return false if the GameCanvas is invisible and vice versa. Once the GameCanvas becomes visible, it will initially indicate that all the keys are unpressed(0).
  • The GameCanvas class provides methods for immediate painting and for examining the state of the device keys. These new methods make it possible to enclose the entire functionality of a game in a single loop, under the control of a single thread.
  • TRANS_MIRROR Causes the Sprite to appear reflected about its vertical center
  • Transforms are applied to the original Sprite images; they are not cumulative, nor can they be combined.
  • The Sprite.defineCollisionRectangle(int x, int y, int w, int h) defines the Sprite’s bounding rectangle that is used for collision detection purposes.
  • A Sprite object has 7 transformations.
  • Calling the setImage(Image image, int frameWidth, int frameHeight) will cause a new frame set to be loaded into the Sprite.

    Ø If the new Image contains more frames than the current Image

    - The current frame remains unchanged.

    - If the defined frame sequence is a custom sequence, it remains unchanged. If it is default, the frame sequence will be reset to the default sequence of the NEW Image.

    Ø If the new Image contains less frames than the current Image

    - The current frame will be set to the 0th frame.

    - The frame sequence will be set to the default frame sequence of the new Image, regardless of whether the current frame seqeuence is default or not.

    Ø The reference pixel is unchanged in its definition as well as its location.

    - If the frame size is different, the top-left co-ordinate will be shifted such that the reference pixel will remain stationary.

    - If the frame size is different, the collision rectangle will be reset to the new size of the Sprite (which would be the size of the frame).

  • Sprite is been added in MIDP 2.0, Frame sequence of sprite class starts from 0, The developer must programatically switch the current frame in the frame sequence of Sprite in order to get animation affect.
  • When a transform is applied, the Sprite is automatically repositioned such that the reference pixel appears stationary in the painter's coordinate system. Thus, the reference pixel effectively becomes the center of the transform operation. Since the reference pixel does not move, the values returned by getRefPixelX() and getRefPixelY() remain the same; however, the values returned by getX() and getY() may change to reflect the movement of the Sprite's upper-left corner.
  • In Sprite class the frames have to be switched manually using the nextFrame() and prevFrame() methods. This switching is circular and so calling preFrame() on the first frame will give the last frame in the sequence and vice versa. The frame located in the upper-left corner of the Image is first assigned an index of 0. The remaining frames are then numbered consecutively in row-major order.

Ø The Layer appended will have the highest index (farthest from user).

Ø If the Layer is previously present, it will be removed.

Ø Start at 0

Read more!

No comments: