Search | Statistics | User Listing Forums
XNA Resources
XNA Resources ->  XNAResources.com Tutorial Forums -> Star Defense Series -> View Thread

You are logged in as a guest. ( logon | register )

Begin must be called successfully before a draw can be called - Part 2 problem
Jump to page : 1
Now viewing page 1 [25 messages per page]
View previous thread :: View next thread
   XNAResources.com Tutorial Forums -> Star Defense SeriesMessage format
 
unijaw
Posted 2012-11-01 11:16 PM (#531)
Subject: Begin must be called successfully before a draw can be called - Part 2 problem


New user

Posts: 2


(77.102.250.133)
Hello! I'm sure this is a very obvious mistake but I am unable to put a animated sprite to the screen as it prevents me from doing so saying that 'Begin must be called successfully before a draw can be called' This is not an error but does appear to be a warning it does however prevent me from running the program. here is the code in question


public void Draw(
SpriteBatch spriteBatch,
int XOffset,
int YOffset,
bool NeedBeginEnd)
{
if (NeedBeginEnd)
spriteBatch.Begin();

spriteBatch.Draw(
t2dTexture,
new Rectangle(
iScreenX + XOffset,
iScreenY + YOffset,
iFrameWidth,
iFrameHeight),
GetSourceRect(),
Color.White);

if (NeedBeginEnd)
spriteBatch.End();
}

public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset)
{
Draw(spriteBatch, XOffset, YOffset, true);
}

Any advice would be great thanks
Top of the page Bottom of the page
gegillam
Posted 2013-02-22 3:19 AM (#549 - in reply to #531)
Subject: Re: Begin must be called successfully before a draw can be called - Part 2 problem


New user

Posts: 1


(71.207.133.76)
I think your problem is coming from your last four lines. Notice in your first Draw method, you do a spritebatch.Begin();, and in your second draw overload you don't. I'm not sure how to fix it but that's where the code starts to look wonky to me.

I could be wrong, as I am a beginner and disclaimer: I know nothing. I haven't seen an 'underloaded' method call an overloaded method of its self, and I find this code difficult to read because the variables that are really different all have the same names where they're being passed.

public void Draw(
SpriteBatch spriteBatch,
int XOffset,
int YOffset,
bool NeedBeginEnd)
{
if (NeedBeginEnd)
spriteBatch.Begin();

spriteBatch.Draw(
t2dTexture,
new Rectangle(
iScreenX + XOffset,
iScreenY + YOffset,
iFrameWidth,
iFrameHeight),
GetSourceRect(),
Color.White);

if (NeedBeginEnd)
spriteBatch.End();
}

public void Draw(SpriteBatch aSpriteBatch, int aXOffset, int aYOffset)
{
Draw(aSpriteBatch, aXOffset, aYOffset, true);
}

something like that might be easier... also why is this overload just adding true to a set of arguments? Why not just type out true inline?

Edited by gegillam 2013-02-22 3:28 AM
Top of the page Bottom of the page
Jump to page : 1
Now viewing page 1 [25 messages per page]
Jump to forum :
Search this forum
Printer friendly version
E-mail a link to this thread

(Delete all cookies set by this site)
Running MegaBBS ASP Forum Software
© 2002-2017 PD9 Software