Please login or register.

Login with username, password and session length
Advanced search  

News:

For WME related articles and tutorials visit WME Resource Center.

Author Topic: Having problems controlling Sprite frames in my in-game puzzle  (Read 5709 times)

0 Members and 1 Guest are viewing this topic.

warmblood

  • Supporter
  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Female
  • Posts: 18
    • View Profile

Okay, I've lurked long enough, but I haven't been idle. I've read a lot of the forums, and gone through the tuts, and made my own little test games.

But now I'm stumped. I'm trying to create a mini-game -- essentially memory match (where you turn cards looking for matching pairs).  

 :D  I am able to dynamically create the "card" entities objects (thanks to metaphorium's Going dynamical - Learn by code example: http://forum.dead-code.org/index.php?topic=1821.0).

 :(  Sadly, I think I'm missing something/doing something dumb:

  • I can't control the sprite frames properly -- the first card should show frame 1 when it is turned over, the second should show frame 2, etc. When you click them again, they should show frame 0 (the card back,)  Instead, they start out properly showing frame 0, then end up showing frame 3 or frame 4.
     
  • The frames don't seem to change at all unless I have a Debug(); statement in my Card script so I can step through the code.


If anyone can figure out what I'm doing wrong, I'd really appreciate your help.  (I know I may have to create a seperate sprite for each card face and switch the sprite, but I was hoping to use the frame approach in the example.)

I've ripped out most of the logic in my code and reduced it down to just what is needed to recreate my problem(s). If it would be easier to have the actual example, I have zipped it up, and can email it to you.

1. Created a new game.
2. Created a new scene called MemoryMatch, and make it the starting scene.
3. In the scene, created a sprite called Card. Add five frames (each 60x60 or you have to change the spacing in scene_init). Frame 0 is the card back, and the other frames are the card fronts. Create a script for this sprite containing the following code:

Code: [Select]


#include "scripts\base.inc"


////////////////////////////////////////////////////////////////////////////////
// This is where I suspect the problems are

on "LeftClick"
{

//PROBLEM: if you don't step thru code in debugger, card frames don't appear to change at all.
Debug();  

var entCard = this;
if (entCard.Active == false) return;  // the card is not active, so don't change it

var sprCard = entCard.GetSpriteObject();

if (entCard.currentFrame==0)
{
//The card is face down, so turn it face up.  
//PROBLEM: Can't seem to control the frame of the sprite.
this.sprite = sprCard.Play();
this.sprite = sprCard.GetFrame(entCard.faceFrame);
this.sprite = sprCard.Pause();
entCard.currentFrame=entCard.faceFrame;
}
else
{
//The card is face up, so turn it face down.
//PROBLEM: Can't seem to control the frame of the sprite.
this.sprite = sprCard.Play();
this.sprite = sprCard.GetFrame(0);
this.sprite = sprCard.Pause();
entCard.currentFrame=0;
}
}


// This is called from scene_init to set up the sprite entities.
method InitCard(name, cardNo, posX, posY)
{

// Create dynamic sprite entity
this.Ent = Scene.CreateEntity(name);
var entCard = this.Ent;
entCard.AttachScript("scenes\MemoryMatch\scr\Card.script");

// load Card sprite into the dynamic sprite entity
entCard.SetSprite("scenes\MemoryMatch\Cards\Card.sprite");

// store the card number and face frame
entCard.cardNo=cardNo;
entCard.faceFrame=cardNo;
 
// make card iteractive and non-scalable
entCard.Scalable=false;
entCard.Interactive=true;

//set the current frame to card back (face down).
var sprCard = entCard.GetSpriteObject();
this.sprite = sprCard.GetFrame(0);
this.sprite = sprCard.Pause();
entCard.currentFrame=0;

// position the entity in the scene
entCard.X = posX;
entCard.Y = posY;

// Make sure the card is visible
entCard.Active = true;
}




4. Added the following code to the end of scene_init.script:


Code: [Select]
////////////////////////////////////////////////////////////////////////////////
// Add the cards...

var numCards = 4;  
var posX, posY;
var cardNo, faceFrame;

var cardDeckArray=new Array(numCards);

posY=100;
for (cardNo=0;cardNo<numCards;cardNo=cardNo+1)
{
posX=60 + cardNo*(61);  // change this line if your sprites are bigger than 60 pixels wide
faceFrame=cardNo;

cardDeckArray[cardNo] = new Object("scenes/MemoryMatch/scr/Card.script");  
var tmp = cardDeckArray[cardNo];  

tmp.InitCard("Card" + cardNo, cardNo, posX, posY);

cardDeckArray[cardNo]=tmp;
}


5. When you run the program, four "card" sprite entities will appear site by side, all "face down."  Click on one of the sprite entities and you'll hit the debugger (assuming you have debug turned on and are in window mode). Step through the code for the Left Click event, and the cards should flip face up. But then as you continue stepping the face keeps changing.  Click on the card again, and it is supposed to turn face down, but it doesn't...

Logged

Daniel

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 124
  • I'm *not* a llama!
    • View Profile
Re: Having problems controlling Sprite frames in my in-game puzzle
« Reply #1 on: June 18, 2008, 08:27:04 AM »

I haven't really tested your code but by just going quickly through it I did notice something that might prove problematic if I understand what you're trying to do.

In general, if I want to use a sprite just as a container for frames and to manually switch between them on my own and never use it as an actual animation, I Pause the sprite once and never Play/Pause it again. Whenever I want to change the frame I just set the CurrentFrame to the one I want and that's it. If you actually Play the sprite you lose your manual control over the frames.

This is just a general tip, I'm not sure if this is actually your problem.
Logged

warmblood

  • Supporter
  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Female
  • Posts: 18
    • View Profile
Re: Having problems controlling Sprite frames in my in-game puzzle
« Reply #2 on: June 18, 2008, 10:29:10 AM »

I went ahead and got this running with seperate sprites, so there is no great urgency to the above problem. I'm still curious if it is possible to control the frames better.  I tried increasing the frame delay, but that didn't help.

Anyway, I plan to post my mini-game as a tutorial soon.  I learned a lot by doing it, and hopefully others will, too. I'd appreciate if someone would volunteer to give it a look before I post it to make sure I'm not telling people to do something wrong.

 
Logged

Daniel

  • Regular poster
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 124
  • I'm *not* a llama!
    • View Profile
Re: Having problems controlling Sprite frames in my in-game puzzle
« Reply #3 on: June 18, 2008, 11:36:59 AM »

I'm still curious if it is possible to control the frames better.

You can perfectly control which frame is displayed simply by setting CurrentFrame to whatever frame index you want. It should work perfectly as long as you never call the Play method.
Logged

warmblood

  • Supporter
  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Female
  • Posts: 18
    • View Profile
Re: Having problems controlling Sprite frames in my in-game puzzle
« Reply #4 on: June 21, 2008, 11:42:46 AM »

Thanks, Daniel. Yes, I am able to get it to work using something like this, which just switches from frame 0 to frame 1 when the object is left clicked:

Code: [Select]
on "LeftClick"
{
sprCard=this.GetSpriteObject();

if (sprCard.CurrentFrame==1) sprCard.CurrentFrame=0;
else sprCard.CurrentFrame=0;
}

 :D It is so much more elegant to have one "card" sprite with n different faces, as opposed to n card sprites each with one face.
Logged
 

Page created in 0.02 seconds with 24 queries.