System.InvalidOperationException in WP 8 Game (MonoGame ) - xna

So this is my Game1 class :
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GameName2
{
public class Game1 : Game
{
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
Texture2D _bg;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
_bg = Content.Load<Texture2D>(#"Loading");
// TODO: use this.Content to load your game content here
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
_spriteBatch.Draw(_bg,
new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
null,
Color.White,
0,
Vector2.Zero,
SpriteEffects.None,
0);
base.Draw(gameTime);
}
}
}
I make a "Content" folder in my project, and add Loading.xnb as existing item. And then, I change the Build Action of the Loading.xnb to "Content" and the Copy to Output as "Copy Always".
But when I compile it, this section throw System.InvalidOperationException
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
_spriteBatch.Draw(_bg,
new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
null,
Color.White,
0,
Vector2.Zero,
SpriteEffects.None,
0);
base.Draw(gameTime);
}
Specifically at the _spriteBatch.Draw(.......) method. Can somebody help me? Thanks.

Look at some of the example programs. _spriteBatch.Draw must be called between _spriteBatch.Begin and _spriteBatch.End.
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
_spriteBatch.Begin();
_spriteBatch.Draw(_bg,
new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
null,
Color.White,
0,
Vector2.Zero,
SpriteEffects.None,
0);
_spriteBatch.End();
base.Draw(gameTime);

Related

Custom Pop up with no Black background in blackberry

I want to remove the black background in Popup field
i know we use applyTheme method in blackberry to subdue its effect but dunno how to use it
I want to remove the black background and use an image instead .
I have tried this method
protected void applyTheme(Graphics arg0, boolean arg1) {
// TODO Auto-generated method stub
super.applyTheme(arg0, arg1);
}
public class CustomDialogBox extends PopupScreen {
Bitmap mDialogImg=null;
public CustomDialogBox(Bitmap dialogImg) {
super(new VerticalFieldManager(),Field.FOCUSABLE);
this.mDialogImg=dialogImg;
VerticalFieldManager vfm=new VerticalFieldManager() {
protected void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, mDialogImg.getWidth(), mDialogImg.getHeight(), mDialogImg, 0, 0);
};
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(mDialogImg.getWidth(), mDialogImg.getHeight());
super.setExtent(mDialogImg.getWidth(), mDialogImg.getHeight());
}
};
add(vfm);
}
protected void applyTheme() {
}
}
I tried the following program and it works perfectly fine
i added a Bitmap image to a vertical field manager and then
using the method
applyTheme() as follows
protected void applyTheme() {
}
i do get the required results

Set Background Image in Header in Blackberry?

I want to add header.In header,i want to add background image..and two buttons.. in Blackberry.
so what i have to do??
I have tried following.. but it did not work..
VerticalFieldManager vfm=new VerticalFieldManager();
vfm.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("topbar.png")));
setTitle(vfm);
try this -
final Bitmap bg = Bitmap.getBitmapResource("bg.png");
VerticalFieldManager vfm_mid = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x040811);
graphics.clear();
graphics.drawBitmap(0, 0, bg.getWidth(),
bg.getHeight(), bg, 0, 0);
super.paint(graphics);
}
};
ButtonField b1=new ButtonField("Button 1");
ButtonField b2=new ButtonField("Button 2");
vfm_mid.add(b1);
vfm_mid.add(b2);
setTitle(vfm_mid);
try this custom header:
public class TopBar extends HorizontalFieldManager implements FieldChangeListener{
int width;
int height;
Bitmap bgBitmap;
Button btnHome;
Bitmap btnImage;
boolean isHome;
MainScreen screenFromNavigate;
LabelField lblTitle;
public TopBar(String title)
{
super(FIELD_HCENTER);
setLayout(title);
}
public void setLayout(String title)
{
this.width=Display.getWidth();
this.height=50;
this.bgBitmap= Bitmap.getBitmapResource("topbar.png");
this.isHome = isHomeBtn;
btnHome = new ButtonField("Button 1");
btnHome.setMargin(7, 0, 0, 15);
add(btnHome);
btnHome.setChangeListener(this);
lblTitle = new LabelField(title);
add(lblTitle);
lblTitle.setMargin(12, 0, 0,Display.getWidth()/2-160);
}
protected void sublayout(int maxWidth, int maxHeight)
{
maxWidth=width;
maxHeight=height;
super.sublayout(maxWidth, maxHeight);
setExtent(maxWidth, maxHeight);
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(),50, bgBitmap, 0, 0);
// graphics.setBackgroundColor(Color.GRAY);
// graphics.clear();
super.paint(graphics);
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
}
}
}
call this:
TopBar topBar = new TopBar("Test");
setTitle(topBar);

Custom PopUp to work like tooltip without time out

I need to create a Customised PopUp to work like a ToolTip How can i achieve it
i have worked on a code please check out and tell me where i need improvement
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class ToolTip extends PopupScreen{
private static VerticalFieldManager vfm;
private LabelField lbl;
private int xPosition;
private int yPosition;
private String message;
public ToolTip(String message,int xPos,int yPos){
super(vfm);
this.xPosition=xPos;
this.yPosition=yPos;
this.message=message;
vfm=new VerticalFieldManager(){
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0,0,getWidth(),getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0,0,getWidth(),getHeight());
super.paint(graphics);
}
};
lbl=new LabelField(message);
vfm.add(lbl);
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setExtent(lbl.getPreferredWidth(), lbl.getPreferredHeight());
setPosition(xPosition, yPosition);
}
}
i get an error of NullPointer Exception at super(vfm) because vfm is null when i use it as follows . How can i optimise my code .
ButtonField bf1=new ButtonField("Login"){
protected boolean navigationClick(int status, int time) {
UiApplication.getUiApplication().pushScreen(new ToolTip("Hello ", 50, 50));
return super.navigationClick(status, time);
}
};
add(bf1);
Pass an initialized instance of VerticalFieldManager to super(..) of PopupScreen. Check following code.
class ToolTip extends PopupScreen {
private static VerticalFieldManager vfm = new VerticalFieldManager() {
protected void paint(Graphics graphics) {
graphics.setColor(0x00FFFFFF);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(0x00000000);
graphics.drawRect(0, 0, getWidth(), getHeight());
super.paint(graphics);
}
};
// other codes
public ToolTip(String message, int xPos, int yPos) {
super(vfm);
// other codes
}
protected void sublayout(int width, int height) {
// other codes
}
}
Updated code for custom background support
class ToolTip extends PopupScreen {
private int xPosition;
private int yPosition;
public ToolTip(String message, int xPos, int yPos) {
super(new VerticalFieldManager());
// Define and set background here
// Use - BackgroundFactory.createBitmapBackground(bitmap) for bitmap background
getDelegate().setBackground(BackgroundFactory.createSolidBackground(Color.RED));
// Add other UI Field here
this.xPosition=xPos;
this.yPosition=yPos;
add(new LabelField(message));
// other codes
}
// Empty implementation of this will disable default rounded border.
protected void applyTheme() {
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
setPosition(xPosition, yPosition);
}
}
You need to use getDelegate().setBackground(..) to set the background. Check the class BackgroundFactory for creating custom background.

How to change background color of object choice field in blackberry?

I want to change background color of selected item in object choice Field in blackberry?
Read the BlackBerry API documents for Field.setBackground(Background background), Field.setBackground(int visual, Background background) and Background.
Try like this sample code:
public class SamplePopupScreen extends PopupScreen
{
BitmapField bitmapField[];
Bitmap bitmap=Bitmap.getBitmapResource("box-s6A.png");
public SamplePopupScreen()
{
super(new VerticalFieldManager(),PopupScreen.DEFAULT_CLOSE);
createGUI();
}
private void createGUI()
{
bitmapField=new BitmapField[3];
add(new LabelField("Popup Screen",Field.FIELD_HCENTER));
for(int i=0;i<3;i++)
{
VerticalFieldManager vr=new VerticalFieldManager(Field.FIELD_HCENTER|VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
bitmapField[i]=new BitmapField(null,Field.FOCUSABLE|Field.FIELD_HCENTER)
{
protected void paint(Graphics g)
{
g.clear();
g.drawText("LabelNunber", 0, 0);
if(isFocus())
{
g.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);
g.drawText("LabelNunber",0,0);
}
}
protected void layout(int width, int height)
{
super.layout(250, 50);
setExtent(250, 50);
}
};
vr.add(bitmapField[i]);
add(vr);
}
}
}
and I am getting like this:

blackberery popup windows without black border color

hi all The below is my code .i try to remove black border color from popup window .i tried this but still i get small black border .
public class S2SPopup extends PopupScreen
{
public S2SPopup()
{
super(new VerticalFieldManager()
{
public void paint(Graphics g)
{
int a = g.getGlobalAlpha();
int c = g.getColor();
g.setColor(c);
g.drawBitmap(0, 0, HomeScreen.popupimg.getWidth(),HomeScreen.popupimg.getHeight(),HomeScreen.popupimg, 0, 0);
super.paint(g);
}
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout( HomeScreen.popupimg.getWidth(),HomeScreen.popupimg.getHeight());
setExtent( HomeScreen.popupimg.getWidth(),HomeScreen.popupimg.getHeight());
}
});
setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 0));
VerticalFieldManager vfm_Label = new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth,150);
setExtent(maxWidth, 150);
}
};
vfm_Label.setMargin(80, 0, 0, 0);
ButtonField btnclose = new ButtonField("",ButtonField.RIGHT)
{
public void paint(Graphics g)
{
//setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 0));
int c = g.getGlobalAlpha();
g.setBackgroundColor(c);
super.paint(g);
}
};
btnclose.setMargin(0, 0, 0, 250);
btnclose.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context)
{
close();
}
});
LabelField s2sLabelField = new LabelField()
{
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
super.paint(g);
}
protected void layout(int width, int height)
{
super.layout(width, 150);
setExtent(width, 150);
}
};
s2sLabelField.setPadding(0, 50, 0, 30);
s2sLabelField.setText("lasjdfljlsjlfj ljsfdl jsflljfiowurnowncnvouern.zvovn ljlsfdjj jlj" +
"jsljfdlj ljsfl sjfl jfjsljdfljslfu jsjf;ujerpljsfdjpn sdflsajf ss23s mail jsldfjlfdju nsfjljljlfjmnn,nsf,n,nlojljlsndf,n,ljnsjfdljjufsn jj" +
"sjfd;jjljlsduflja;sfj ljsldfujrqnfqperiujf.zvnpqrue 33333333333333333333333333333333333333 ");
add(btnclose);
vfm_Label.add(s2sLabelField);
add(vfm_Label);
} //end of constructor
} // end of main screen
Override applyTheme with an empty implementation, and the black border goes away. You can't fix this from the constructor alone:
protected void applyTheme(){}
I doubt that it will correct this, but you may try giving your delegate manager a margin of 0 just to be certain that there isn't one on it and background color is creeping in from the theme. A solution that I have implemented in the past is to create your own custom Screen
public class CustomPopup extends Screen {
public CustomPopup() {
super(new VerticalFieldManager() {
//your custom code or a custom manager class (which is what I did)
);
setBackground(BackgroundFactory.createSolidTransparentBackground(0x000000, 0));
}
protected void sublayout(int width, int height) {
//make this screen take up the entire display
setPosition(0, 0);
setExtent(Display.getWidth(), Display.getHeight());
//and layout the delegate
setPositionDelegate(some_x, some_y);
layoutDelegate(some_width, some_height);
}
}
You'll have to add your own buttons and other graphics to it, but This should at least help point you in the right direction.

Resources