2015-06-16 4 views
6

Ho creato una griglia 10x10 e ora vorrei posizionare un'immagine del giocatore all'interno di uno dei quadrati della griglia. per fare questo penso di dover usare bitmap? Metterò tutto il mio codice qui sotto. per provare a farlo comunque ho creato una classe di gioco che imposta e ottiene la posizione x dell'immagine del giocatore. Ho quindi creato una classe giocatore, che è dove cerco di aggiungere l'immagine, usando Bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.sokobanplayer1); Continuo a ricevere un errore sotto getResources così ho creato un metodo per ottenere risorse in gioco, ma non ha risolto il problema. qualcuno potrebbe per favore mostrarmi come farlo, il mio codice è sotto.Android: Errore durante l'utilizzo di bitmap per visualizzare l'immagine con 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() – playmaker420

+0

passare il contesto al costruttore del vostro classe Player – playmaker420

+0

che elimina l'errore, ma l'immagine ha colpiti la visualizzazione su la griglia? perchè è questo? – Phill

risposta

0

Sostituisci il tuo Player classe con questo:

//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); 
    } 
} 
Problemi correlati