2015-06-16 11 views
6

Stworzyłem siatkę 10x10, a teraz chciałbym umieścić obraz gracza wewnątrz jednego z kwadratów siatki. aby to zrobić, myślę, że muszę użyć bitmapy? Podam cały mój kod poniżej. aby spróbować to zrobić, stworzyłem klasę gier, która ustawia i dostaje pozycję x obrazu gracza. Następnie utworzyłem klasę gracza, w której próbuję dodać obraz, używając bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.sokobanplayer1); Ciągle otrzymuję błąd w getResources, więc utworzyłem metodę pobierania zasobów w grze, ale nie rozwiązał problemu. czy ktoś mógłby mi pokazać, jak to zrobić, mój kod jest poniżej.Android: Błąd podczas używania bitmapy do wyświetlania obrazu za pomocą Android Studio

//DRAW CLASS 
public class Draw extends View { 
Paint red = new Paint(); 
Paint green = new Paint(); 

int rectSide = 1000; 

public Draw(Context context) { 
    super(context); 
    red.setColor(Color.RED); 
    green.setColor(Color.GREEN); 
    red.setStrokeWidth(8); 
    green.setStrokeWidth(8); 
} 

public void drawGrid(Canvas canvas) { 

    int width = canvas.getWidth(); 
    int height = canvas.getHeight(); 


    float startX = (width/2) - (rectSide/2); 
    float stopX = (width/2) + (rectSide/2); 
    float startY = (height/2) - (rectSide/2); 
    float stopY = (height/2) + (rectSide/2); 

    for (int i = 0; i < 1100; i += 100) { 
     float newY = startY + i; 
     canvas.drawLine(startX, newY, stopX, newY, green); 
    } 

    for (int i = 0; i < 1100; i += 100) { 

     float newX = startX + i; 
     canvas.drawLine(newX, startY, newX, stopY, green); 
    } 
} 

@Override 
public void onDraw(Canvas canvas) { 

    drawGrid(canvas); 
} 
} 

//GAME CLASS 
public class Game { 

Bitmap image; 
int x; 
int y; 
int height; 
int width; 


public void setX(int x){ 
    this.x = x; 
} 

public void setY(int y){ 
    this.y = y; 
} 

public int getX(){ 
    return x; 
} 

public int getY(){ 
    return y; 
} 

public int getHeight(){ 
    return height; 
} 

public int getWidth(){ 
    return width; 
} 

public Bitmap getResources(){ 
    return image; 
} 

} 


//PLAYER CLASS 
public class Player extends Game { 

public Player(Bitmap res, int w, int h){ 

    image = res; 
    x = 300; 
    y = 300; 
    height = h; 
    width = w; 

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1); 
} 
} 


//MAIN ACTIVITY 
public class MainActivity extends Activity { 
Draw draw; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_main); 

    draw = new Draw(this); 
    draw.setBackgroundColor(Color.BLUE); 
    setContentView(draw); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 


} 
+0

context.getResources Pass (Pass) – playmaker420

+0

kontekst do klasy gracza konstruktora – playmaker420

+0

że pozbywa się błędu, ale obraz nie robi wyświetlenia na siatka? dlaczego to? – Phill

Odpowiedz

0

Wymień gracza klasę z tym:

//PLAYER CLASS 
class Player extends Game { 

    public Player(Context context, int w, int h){ 

     x = 300; 
     y = 300; 
     height = h; 
     width = w; 

     image = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1); 
    } 
} 
Powiązane problemy