Java Question getView

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hi,

I'm creating a new library and I want to move a programmatically added imageview x pixels to the left.

B4X:
ImageViewWrapper imgObject = new ImageViewWrapper();
      imgObject.Initialize(ba, "imgObject");
      imgObject.setBitmap(GameObject.getBitmap());
      Log.i("B4A","GameFramework: " + imgObject.getObject().getId());
      GameObject.setXPosition(x);
      GameObject.setYPosition(y);
      this.DrawingPanel.AddView(imgObject.getObject(), x, y, GameObject.getBitmap().getHeight(), GameObject.getBitmap().getHeight());
      GameObject.setID(imgObject.getObject().getId());

B4X:
/**
    * Gets or sets the ID of this game object.
    * WARNING! Not recommended to change the id!
    * An id will be given when the object is placed on the drawing panel.
    */
   public int getID() {
      return this.id;
   }
   
   /**
    * Gets or sets the ID of this game object.
    * WARNING! Not recommended to change the id!
    * An id will be given when the object is placed on the drawing panel.
    */
   public void setID(int id) {
      this.id = id;
   }


B4X:
/**
    * Moves the Game Object to the left.
    * X - the amount of pixels to go left
    */
   public void MoveLeft(int x) {
      this.GameObject.setXPosition(this.GameObject.getXPosition()-x);
      this.GameEngine.getDrawingPanel().GetView(GameObject.getID()).setLeft(GameObject.getXPosition()-x);
   }

Now i'm receiving a RuntimeException at MoveLeft:

Object should first be initialized.

The view should be initialized with ImageViewWrapper imgObject = new ImageViewWrapper();
imgObject.Initialize(ba, "imgObject"); and the id log gives me id: 2.

DrawingPanel is a Panel from B4A.

Any help?

Kind regards,
Tomas
 
Last edited:

XverhelstX

Well-Known Member
Licensed User
Longtime User
Apparently, the view id is 2, but on the panel it is 0 ?? (I checked with NumberOfViews saying it has 1 view.)

Tomas

EDIT: Now instead, i need to change the bitmap of an imageview.
With :
B4X:
this.GameEngine.getDrawingPanel().GetView(GameObject.getID()-2)

I cannot seem to be able to call .setBitmap because it's a concreteviewwrapper. Is there a way to cast from the concreteViewWrapper to an ImageViewWrapper or just change the bitmap with setBitmap?

Tomas
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
Where does the concreteviewwrapper come from? I think something is wrong.
You dont have the correct view which should be the ImageViewWrapper.
Also, an alternative is to try to keep a reference to the image wrapper rather than trying to get it from the panel.


btw...i doubt your approach with imageviews for sprites. it may work but please think about overlap, interaction and collision detection in the sprites.
 
Top