Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I ended up doing. Hopefully this helps some other beginners out there get started.</p> <p>I have a Sprite class that represents the object I want to move on the screen:</p> <pre><code> public class Sprite { private final String TAG = "Sprite"; private Drawable drawable; private int x; // the X coordinate private int y; // the Y coordinate private boolean touched; // if droid is touched/picked up private Speed speed; // the speed with its directions public Sprite(Drawable drawable, int x, int y) { this.drawable = drawable; this.x = x; this.y = y; this.speed = new Speed(); } public void draw(Canvas canvas) { drawable.setBounds(new Rect(x, y, x+drawable.getIntrinsicWidth(), y+drawable.getIntrinsicHeight())); drawable.draw(canvas); } public void move() { if (!touched) { x += (speed.getXv() * speed.getxDirection()); y += (speed.getYv() * speed.getyDirection()); } } public void handleActionDown(int eventX, int eventY) { if (eventX &gt;= (x - bitmap.getWidth() / 2) &amp;&amp; (eventX &lt;= (x + bitmap.getWidth() / 2))) { if (eventY &gt;= (y - bitmap.getHeight() / 2) &amp;&amp; (y &lt;= (y + bitmap.getHeight() / 2))) { // droid touched setTouched(true); } else { setTouched(false); } } else { setTouched(false); } } } </code></pre> <p>Then I have a main game loop. That loops through and calls my mainPanel's render and update methods which look like this:</p> <pre><code> public void render(Canvas canvas) { canvas.drawColor(Color.BLACK); sprite.draw(canvas); } public void update() { sprite.move(); } </code></pre> <p>The position of where the sprite will move is handled in the motion event capture:</p> <pre><code> if (event.getAction() == MotionEvent.ACTION_MOVE) { // the gestures if (sprite.isTouched()) { sprite.setX((int) event.getX()); sprite.setY((int) event.getY()); } } </code></pre> <p>Hopefully that is helpful. If I trimmed out too much and there is something you don't get let me know. </p> <p>Next step, making the object follow the line!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload