A.O.92
A.O.92

Reputation: 11

Path Line using in andengine

I want to make a paint game in andengine. There is my codes. How can i use them in andengine? Or is there anything like drawPath in andengine? I tried to add Rects or Lines for drawing but my FPS was 10-15.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SingleTouchEventView extends View {
  private Paint paint = new Paint();
  private Path path = new Path();

  public SingleTouchEventView(Context context, AttributeSet attrs) {
    super(context, attrs);

    setBackgroundColor(Color.WHITE);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      path.moveTo(eventX, eventY);
      return true;
    case MotionEvent.ACTION_MOVE:
      path.lineTo(eventX, eventY);
      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      break;
    default:
      return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
  }

Upvotes: -1

Views: 112

Answers (1)

sjkm
sjkm

Reputation: 3937

There are several ways to accomplish what you want.

  1. create Rectangles (not lines since they differ from device to device) and add them to the scene to get a path. Use object pools to reuse your objects (Rectangles).

  2. if the first approach doesn't perform well then you could also draw directly on an empty texture using canvas.

  3. there is a class called RenderTexture on which you can draw entities. Use such a RenderTexture and draw your lines to it. create a sprite using this Texture and add it to the scene.

Upvotes: 0

Related Questions