Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.gamemaker.GameObjects; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; /** * Created by cjx on 2016/7/4. */ public class Scrollable { // Protected is similar to private, but allows inheritance by subclasses. protected Vector2 position; protected Vector2 velocity; protected int width; protected int height; protected boolean isScrolledLeft; protected Vector2 acceleration; protected float distance; protected Rectangle BoundRect; private float acc, MaxSpeed; public Scrollable(float x, float y, int width, int height, float scrollSpeed) { position = new Vector2(x, y); velocity = new Vector2(scrollSpeed, 0); this.width = width; this.height = height; isScrolledLeft = false; acc = 200; MaxSpeed = 400; acceleration = new Vector2(acc, 0); distance = 0; BoundRect = new Rectangle(x, y, (float) width, (float) height); } public void update(float delta) { velocity.add(acceleration.cpy().scl(delta)); if (velocity.x >= -20) { velocity.x = 0; } else if (velocity.x <= -MaxSpeed) { velocity.x = -MaxSpeed; } position.add(velocity.cpy().scl(delta)); BoundRect.set(position.x, position.y, (float) width, (float) height); distance += (0 - velocity.x) * delta; // If the Scrollable object is no longer visible: if (position.x + width < 0) { isScrolledLeft = true; } } // Reset: Should Override in subclass for more specific behavior. public void reset(float newX) { position.x = newX; isScrolledLeft = false; } public void resetItem(float newX, float newY) { position.x = newX; position.y = newY; isScrolledLeft = false; } public void stop() { velocity.x = 0; } // Getters for instance variables public boolean isScrolledLeft() { return isScrolledLeft; } public float getTailX() { return position.x + width; } public void setPosition(float x, float y) { position.set(x, y); } public void setAcceleration(float x, float y) { acceleration.set(x, y); } public void setVelocity(float x, float y) { velocity.set(x, y); } public float getX() { return position.x; } public float getY() { return position.y; } public int getWidth() { return width; } public int getHeight() { return height; } public float getDistance() { return distance; } public Rectangle getBoundRect() { return BoundRect; } public boolean isRunning() { return velocity.x < 0; } public float getVelocityX() { return velocity.x; } public void changeSpeed(int speed) { velocity.add(speed, 0); } public void changeAcceleration(float acce) { acc = acce; } public void changeMaxSpeed(float acce) { MaxSpeed = acce; } }