Problem with DynamicSprite.CreateFromFile [SOLVED]

Started by Valentine, Thu 11/10/2007 18:25:57

Previous topic - Next topic

Valentine

Hi, I'm trying to use dynamic sprites, but nothing shows up. The code I'm using is;

if (keycode==32) { DynamicSprite* sprite = DynamicSprite.CreateFromFile("player.bmp");
                     RawDrawImage(mouse.x, mouse.x, sprite.Graphic);}

I'm unsure as to which directory this points to, so I've put of copy of player.bmp in both the Compiled folder as well as the game folder, but still nothing shows up. The file I'm pointing to is named player.bmp too.

Whats going wrong here :-\? Thanks.

Ashen

It needs to be in the 'Compiled' folder, not the game folder. That doesn't seem to be the proble, though - if it couldn't find the image file, you'd get a crash with an error message, not just nothing showing up.

Are you definitely using valid (i.e. on-screen) coordinates? For example, the code you posted says mouse.x, mouse.x, where I'd expect the second one to be .y. If you pressed the button while the mouse on the far left of the screen, (x, x) would draw the image out of bounds (e.g. 300,300). Also, if you're using a scrolling room and aren't in the top-right corner of it, the mouse coords might be off screen (RawDrawImage uses ROOM, not SCREEN, coords - use GetViewportX/Y to compensate).

Other than that, that code works fine for me...
I know what you're thinking ... Don't think that.

Valentine

Ah, its because I was using a scrolling room (the mouse.x, mouse.x was just me mistyping my code onto the boards ::). Thanks for setting me right!

Unfortunately, I was wanting to use this to draw something onto the GUI, and in such a way that it moves when the room scrolls, is there any way to do this?


Ashen

#3
Well, you an draw relative to the screen position using GetViewportX/Y, like I said:
Code: ags

RawDrawImage(mouse.x + GetViewportX(), mouse.y + GetViewportY(), sprite.Graphic);


However, RawDrawImage draws below GUIs so you probably won't be able to use it in the way you want. How about declaring the DynamicSprite *sprite outside the on_key_press, grabbing the image as you are doing, and assigning it to a Button or the GUI Background graphic? (You'd need to declare the DynamicSprite seperately, as they're destroyed when the function/condition they're declared in ends - in this case, at the '}' after RawDrawImage.) It depends what exactly you're trying to do, however.
I know what you're thinking ... Don't think that.

Valentine

Okay, I've taken it out of On_Key_Press, and I'm now using this script, but I can't get it to work;

DynamicSprite* PlayerPortrait = DynamicSprite.CreateFromFile("player.bmp");
btnMainPortrait.NormalGraphic = PlayerPortrait.Graphic;

which I've placed in the Game_Start section, although I've moved it to other sections (like Repeatedly_Execute) to see if it would work there, but it doesn't.

Thanks again.

Ashen

#5
Ah, no. It looks like I wasn't clear. Declare the DynamicSprite OUTSIDE of all functions (top of the Global script). Set the sprite wherever you want. The way you've got it, the DynamicSprite is local to game_start - it'll be destroyed as soon as the function's over. Try:
Code: ags

DynamicSprite *PlayerPortrait;


function game_start() {
  PlayerPortrait = DynamicSprite.CreateFromFile("player.bmp");
  btnMainPortrait.NormalGraphic = PlayerPortrait.Graphic;
}


(Works for me.) Looks like you're letting the player have a custom 'avatar' the in game? Nice idea.

You'll also need to delete the DynamicSprite when you quit the game (will probably mean making your own Quit GUI, if you haven't already). I don't think it'll cause any major problems if you don't, but it does generate an error message (error log, when you're not going through the editor).
I know what you're thinking ... Don't think that.

Valentine

It's working now :) Thanks very much! I really appreciate the effort you're going to to help.

And yeah, I'm allowing the player to put in their own avatar, I should be able to bring it up for conversations with a bit of coding.

I get the error when I quit the game, but I'll implement a custom quit game GUI as you say.

Thanks again!

Ashen

#7
Quote
I should be able to bring it up for conversations with a bit of coding.

If you're using 'Seirra-style' or 'QFG4-style' speech, that should be pretty easy. Just create a ViewFrame pointer to Loop 0, Frame 0 of the player's speech view, and set its graphic to PlayerPortrait.Graphic, e.g.:
Code: ags

DynamicSprite *PlayerPortrait;
ViewFrame *Talk;


function game_start() {
  PlayerPortrait = DynamicSprite.CreateFromFile("player.bmp");
  btnMainPortrait.NormalGraphic = PlayerPortrait.Graphic;
  Talk = Game.GetViewFrame(2, 0, 0);
  Talk.Graphic = PlayerPortrait.Graphic;
}


(I don't know if the ViewFrame needs to be declared outside game_start - this is kind of off the top of my head - but I figured better safe than sorry...)

If you're using a custom dialog system, it might be harder.
I know what you're thinking ... Don't think that.

Valentine

Hey cheers man, I'm gonna have a go at coding it myself first - I think I should be able to do it now I know how to import the graphic in the first place - best way to learn really.

Why do I think I'm setting myself up for a post later today saying 'Okay... guess its harder than I thought  ::):P.

Thanks again!

Dualnames

So you 've soldved your problem? Please change the title if you did so we can know.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

SMF spam blocked by CleanTalk