⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sprite.java

📁 j2me is based on j2mepolish, client & server for mobile application. menu sample
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		int refX, refY, colX, colY;
		
		// set the horizontal values:
		if ((this.transform & 2) == 0) {
			// Either TRANS_NONE, TRANS_MIRROR_ROT180, TRANS_MIRROR_ROT270 or TRANS_ROT90
			refX = this.refPixelX;
			colX = this.collisionX;
		} else {
			// Either TRANS_MIRROR, TRANS_ROT180, TRANS_ROT270 or TRANS_MIRROR_ROT90
			refX = this.frameWidth - this.refPixelX;
			colX = this.frameWidth - (this.collisionX + this.collisionWidth);
		}
		
		// set the vertical values:
		if ((this.transform & 1) == 0) {
			// Either TRANS_NONE, TRANS_MIRROR, TRANS_MIRROR_ROT270 or TRANS_ROT270
			refY = this.refPixelY;
			colY = this.collisionY;
		} else {
			// Either TRANS_MIRROR_ROT180, TRANS_ROT180, TRANS_ROT90 or TRANS_MIRROR_ROT90
			refY = this.frameHeight - this.refPixelY;
			colY = this.frameHeight - (this.collisionY + this.collisionHeight);
		}

		if ((this.transform & 4) == 0) {
			// Either TRANS_NONE, TRANS_MIRROR_ROT180, TRANS_MIRROR or TRANS_ROT180
			this.width = this.frameWidth;
			this.height = this.frameHeight;
			this.transformedRefX = refX;
			this.transformedRefY = refY;
			this.transformedCollisionX = colX;
			this.transformedCollisionY = colY;
			this.transformedCollisionWidth = this.collisionWidth;
			this.transformedCollisionHeight = this.collisionHeight;
		} else {
			// Either TRANS_MIRROR_ROT270, TRANS_ROT90, TRANS_ROT270 or TRANS_MIRROR_ROT90
			// the vertical and horizontal values needs to be switched:
			this.width = this.frameHeight;
			this.height = this.frameWidth;
			this.transformedRefX = refY;
			this.transformedRefY = refX;
			this.transformedCollisionX = colY;
			this.transformedCollisionY = colX;
			this.transformedCollisionWidth = this.collisionHeight;
			this.transformedCollisionHeight = this.collisionWidth;
		}
	}

	/**
	 * Sets the transform for this Sprite.  Transforms can be
	 * applied to a Sprite to change its rendered appearance.  Transforms
	 * are applied to the original Sprite image; they are not cumulative,
	 * nor can they be combined.  By default, a Sprite's transform is
	 * <A HREF="../../../../de/enough/polish/ui/game/Sprite.html#TRANS_NONE"><CODE>TRANS_NONE</CODE></A>.
	 * <P>
	 * Since some transforms involve rotations of 90 or 270 degrees, their
	 * use may result in the overall width and height of the Sprite
	 * being swapped.  As a result, the values returned by
	 * <A HREF="../../../../de/enough/polish/ui/game/Layer.html#getWidth()"><CODE>Layer.getWidth()</CODE></A> 
	 * and <A HREF="../../../../de/enough/polish/ui/game/Layer.html#getHeight()"><CODE>Layer.getHeight()</CODE></A> may change.
	 * <p>
	 * The collision rectangle is also modified by the transform so that
	 * it remains static relative to the pixel data of the Sprite.
	 * Similarly, the defined reference pixel is unchanged by this method,
	 * but its visual location within the Sprite may change as a result.
	 * <P>
	 * This method repositions the Sprite so that the location of
	 * the reference pixel in the painter's coordinate system does not change
	 * as a result of changing the transform.  Thus, the reference pixel
	 * effectively becomes the centerpoint for the transform.  Consequently,
	 * the values returned by <A HREF="../../../../de/enough/polish/ui/game/Sprite.html#getRefPixelX()"><CODE>getRefPixelX()</CODE></A> 
	 * and <A HREF="../../../../de/enough/polish/ui/game/Sprite.html#getRefPixelY()"><CODE>getRefPixelY()</CODE></A>
	 * will be the same both before and after the transform is applied, but
	 * the values returned by <A HREF="../../../../de/enough/polish/ui/game/Layer.html#getX()"><CODE>getX()</CODE></A> 
	 * and <A HREF="../../../../de/enough/polish/ui/game/Layer.html#getY()"><CODE>getY()</CODE></A>
	 * may change.
	 * <p>
	 * 
	 * @param transform the desired transform for this Sprite
	 * @throws IllegalArgumentException if the requested transform is invalid
	 * @see #TRANS_NONE
	 * @see #TRANS_ROT90
	 * @see #TRANS_ROT180
	 * @see #TRANS_ROT270
	 * @see #TRANS_MIRROR
	 * @see #TRANS_MIRROR_ROT90
	 * @see #TRANS_MIRROR_ROT180
	 * @see #TRANS_MIRROR_ROT270
	 */
	public void setTransform(int transform)
	{
		this.transform = transform;

		// computes tranformed* values and reposition the sprite.
		int oldRefX = this.transformedRefX, oldRefY = this.transformedRefY;
		applyTransform();
		this.xPosition += oldRefX - this.transformedRefX;
		this.yPosition += oldRefY - this.transformedRefY;
	
		//#ifndef polish.api.siemens-color-game-api
			//#ifndef polish.api.nokia-ui
				// when the nokia-ui is used the frame-dimensions do not need to be changed:
				if (this.rawFrameCount > 1) {
					updateFrame();
				}
			//#endif
		//#endif
		//#if polish.api.nokia-ui && !polish.api.siemens-color-game-api
			//# this.nokiaTransform = NOKIA_TRANSFORM_LOOKUP[transform];
			//# if (this.isSingleFrame) {
				//# updateFrame();
			//# }
		//#endif
	}

	//#ifndef polish.api.siemens-color-game-api
	/**
	 * Checks for a collision between this Sprite and the specified Sprite.
	 * <p>
	 * The J2ME Polish implementation for MIDP/1.0 devices currently does
	 * not support a pixel-level detection. Instead only the defined
	 * collision rectangles are used. 
	 * </p>
	 * <P>
	 * If pixel-level detection is used, a collision is detected only if
	 * opaque pixels collide.  That is, an opaque pixel in the first
	 * Sprite would have to collide with an opaque  pixel in the second
	 * Sprite for a collision to be detected.  Only those pixels within
	 * the Sprites' respective collision rectangles are checked.
	 * </P>
	 * <P>
	 * If pixel-level detection is not used, this method simply
	 * checks if the Sprites' collision rectangles intersect.
	 * </P>
	 * <P>
	 * Any transforms applied to the Sprites are automatically accounted for.
	 * <P>
	 * Both Sprites must be visible in order for a collision to be
	 * detected.
	 * </P>
	 * 
	 * @param s the Sprite to test for collision with
	 * @param pixelLevel true to test for collision on a pixel-by-pixel basis, false to test using simple bounds checking.
	 * 	      The J2ME Polish implementation does not support the pixel-level collision detection.
	 * @return true if the two Sprites have collided, otherwise false
	 * @throws NullPointerException if Sprite s is null
	 */
	public final boolean collidesWith( Sprite s, boolean pixelLevel)
	{
		if (!(this.isVisible && s.isVisible)) {
			return false;
		}
		int enemyX = s.xPosition + s.transformedCollisionX;
		int enemyY = s.yPosition + s.transformedCollisionY; 
		return collidesWith( enemyX, enemyY,
				s.transformedCollisionWidth,
				s.transformedCollisionHeight
				);
	}
	//#endif

	/**
	 * Checks for a collision between this Sprite and the specified TiledLayer.  
	 * 
	 * If pixel-level detection is used, a collision is
	 * detected only if opaque pixels collide.  That is, an opaque pixel in
	 * the Sprite would have to collide with an opaque pixel in TiledLayer
	 * for a collision to be detected.  Only those pixels within the Sprite's
	 * collision rectangle are checked.
	 * <P>
	 * If pixel-level detection is not used, this method simply checks if the
	 * Sprite's collision rectangle intersects with a non-empty cell in the
	 * TiledLayer.
	 * <P>
	 * Any transform applied to the Sprite is automatically accounted for.
	 * <P>
	 * The Sprite and the TiledLayer must both be visible in order for
	 * a collision to be detected.
	 * <P>
	 * 
	 * @param t the TiledLayer to test for collision with
	 * @param pixelLevel true to test for collision on a pixel-by-pixel basis, false to test using simple bounds checking against non-empty cells.
	 * 	      The J2ME Polish implementation does not support the pixel-level collision detection.
	 * @return true if this Sprite has collided with the TiledLayer, otherwise false
	 * @throws NullPointerException - if t is null
	 */
	public final boolean collidesWith( TiledLayer t, boolean pixelLevel)
	{
		//#ifdef polish.api.siemens-color-game-api
			//# if (!(this.isVisible && t.isVisible())) {
				//# return false;
			//# }
			//# int tileWidth = t.getCellWidth();
			//# int tileHeight = t.getCellHeight();
		//#else
			if (!(this.isVisible && t.isVisible)) {
				return false;
			}
			int tileWidth = t.cellWidth;
			int tileHeight = t.cellHeight;
		//#endif
		int cXStart = this.xPosition + this.transformedCollisionX;
		int cXEnd = cXStart + this.transformedCollisionWidth;
		int cYStart = this.yPosition + this.transformedCollisionY;
		int cYEnd = cYStart + this.transformedCollisionHeight;
		int x = cXStart;
		int y = cYStart;
		//#ifdef polish.api.siemens-color-game-api
			//# if (!(this.isVisible && t.isVisible())) {
				//# return false;
			//# }
			//# int tileX = t.getX();
			//# int firstColumn = (cXStart - tileX) / tileWidth;
			//# if (firstColumn < 0) {
				//# x -= (firstColumn * tileWidth);
				//# firstColumn = 0;
			//# }
			//# int tileY = t.getY();
			//# int firstRow = (cYStart - tileY) / tileHeight;
			//# if (firstRow < 0) {
				//# y -= (firstRow * tileHeight );
				//# firstRow = 0;
			//# }
			//# int tileRow = firstRow;
			//# int tileColumn = firstColumn;
			//# while (x <= cXEnd) {
				//# while (y <= cYEnd) {
					//# int tile = t.getCell(tileColumn, tileRow);
					//# if (tile != 0) {
						//# return true;
					//# }
					//# y += tileHeight;
					//# tileRow++;
				//# }
				//# y = cYStart;
				//# tileRow = firstRow;
				//# x += tileWidth;
				//# tileColumn++;
			//# }
			//# return false;
		//#else			
			while (x <= cXEnd) {
				while (y <= cYEnd) {
					int tile = t.getTileAt(x, y);
					if (tile != 0) {
						return true;
					}
					y += tileHeight;
				}
				y = cYStart;
				x += tileWidth;
			}
			return false;
		//#endif
	}

	//#ifndef polish.api.siemens-color-game-api
	/**
	 * Checks for a collision between this Sprite and the specified Image
	 * with its upper left corner at the specified location.  If pixel-level
	 * detection is used, a collision is detected only if opaque pixels
	 * collide.  That is, an opaque pixel in the Sprite would have to collide
	 * with an opaque  pixel in Image for a collision to be detected.  Only
	 * those pixels within the Sprite's collision rectangle are checked.
	 * <P>
	 * If pixel-level detection is not used, this method simply checks if the
	 * Sprite's collision rectangle intersects with the Image's bounds.
	 * <P>
	 * Any transform applied to the Sprite is automatically accounted for.
	 * <P>
	 * The Sprite must be visible in order for a collision to be
	 * detected.
	 * <P>
	 * The J2ME Polish implementation does not support the pixel-level collision detection.
	 * 
	 * @param img the Image to test for collision
	 * @param leftX the horizontal location of the Image's upper left corner
	 * @param topY the vertical location of the Image's upper left corner
	 * @param pixelLevel true to test for collision on a pixel-by-pixel basis, false to test using simple bounds checking;
	 * 	      The J2ME Polish implementation does not support the pixel-level collision detection.
	 * @return true if this Sprite has collided with the Image, otherwise false
	 * @throws NullPointerException if image is null
	 */
	public final boolean collidesWith( Image img, int leftX, int topY, boolean pixelLevel)
	{
		return collidesWith( leftX, topY, img.getWidth(), img.getHeight() );
	}
	//#endif
	
	private boolean collidesWith( int enemyX, int enemyY, int enemyWidth, int enemyHeight ) {
		int cXStart = this.xPosition + this.transformedCollisionX;
		int cXEnd = cXStart + this.transformedCollisionWidth;
		int cYStart = this.yPosition + this.transformedCollisionY;
		int cYEnd = cYStart + this.transformedCollisionHeight;
		/*
		System.out.println("Sprite-Collision: this(" + cXStart + "," + cYStart + ", " + cXEnd + ", " + cYEnd + ")" );
		System.out.println("Sprite-Collision: real(" + this.xPosition + "," + this.yPosition + ")" );
		System.out.println("Sprite-Collision: enem(" + enemyX + "," + enemyY + ", " + (enemyX + enemyWidth) + ", " + (enemyY + enemyHeight) + ")" );
		*/
		if (cYEnd <= enemyY
				|| cYStart >= enemyY + enemyHeight
				|| cXEnd <= enemyX
				|| cXStart >= enemyX + enemyWidth ) 
		{
			return false;
		}
		return true;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -