/** * Copyright (c) 2012-2013 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example.explonoid.game; import javax.microedition.lcdui.game.Sprite; /** * Responsible for moving the ball into different directions and changing its speed. * * For usage, see the class Game. * * @see com.nokia.example.explonoid.game.Game */ public class Ball extends Sprite { private static final int PADDING = 6; private int velocityX; private int velocityY; public Ball(Resources r) { super(r.ball); setCollisionRectangle(r); defineReferencePixel((int) (getWidth() * 0.5 + 0.5), (int) (getHeight() * 0.5 + 0.5)); } private void setCollisionRectangle(Resources r) { int width = this.getWidth(); int padding = r.scale(PADDING); defineCollisionRectangle(padding, padding, width - padding, width - padding); } public void move() { move(getVelocityX(), getVelocityY()); } public void bounceVertically() { velocityY *= -1; } public void bounceHorizontally() { velocityX *= -1; } public void bounceLeft() { if (velocityX > 0) { bounceHorizontally(); } } public void bounceRight() { if (velocityX < 0) { bounceHorizontally(); } } public void bounceUp() { if (velocityY > 0) { bounceVertically(); } } public void bounceDown() { if (velocityY < 0) { bounceVertically(); } } public int getVelocityX() { return velocityX; } public int getVelocityY() { return velocityY; } public void setVelocityX(int velocity) { this.velocityX = velocity; } public void setVelocityY(int velocity) { this.velocityY = velocity; } public void changeVelocityX(int amount) { velocityX += amount; } }