2015-06-16 15 views
6

10x10 ızgara oluşturdum ve şimdi bir oyuncu görüntüsünü ızgara karelerden birine yerleştirmek istiyorum. Bunu yapmak için bitmap kullanmalı mıyım? Tüm kodumu aşağıya koyacağım. Bunu denemek ve yapmak için zaten oyuncu görüntüsünün x konumunu ayarlayan ve alan bir oyun sınıfı yarattım. Daha sonra bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.sokobanplayer1) kullanarak görüntü eklemeyi denediğim bir oyuncu sınıfı oluşturdum; getResources altında bir hata alıyorum, böylece oyunda bir get kaynakları yöntemi oluşturdum, ancak sorunu çözmedi. Birisi bana nasıl yapılacağını gösterebilir, benim kodum aşağıda.Android: android studio kullanarak görüntüyü görüntülemek için bitmap kullanırken hata

//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

geçişli context.getResources() – playmaker420

+0

hata kurtulur sizin Oyuncu sınıfının yapıcı – playmaker420

+0

bağlam geçirin fakat görüntü üzerinde gösterilecek doesnt ızgara? bu neden? – Phill

cevap

0

bu ile Oyuncu sınıfını değiştirin:

//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); 
    } 
} 
İlgili konular