|
-
[Problem] Platform game not working as intended!
Hey!
I'm making a platform game where I plan to make it so that you can move both backwards and forwards in the levels, but if you've killed/picked something up on a level and then go back to that level, it won't be there anymore. I've written some code for it which I thought would work, but it.. doesnt:P. The buggy code is on line ~168-200 in the file(I've tried a couple of variations, one is commented out). It only applies to coin objects so far(or should, anyway ^^), because since it's buggy I didn't bother writing it for the other stuff yet. What I tried is to do is add every objects x&y-value that gets hit by my hero to an array. Then when I start up a level I check if it's been visited before, and if it has I check if any objects has the same x&y-value as those the hero hit last time. If some of them have been hit, the code is supposed to remove them.. but that won't work:P I wondered if anyone would be kind enough to check this out for me and tell me why the code isn't working?
I uploaded the game here, it's 1 MB size.
Thanks in advance and kind regards,
Zak
PS. I added the code here, though I don't know if it makes much sense out of context.. ^^ this is part of the code that runs every time a level is started(and which doesn't work.. :P);
PHP Code:
public function examineLevel() {
treasures = new Array();
for(var i:int=0;i<this.gamelevel.numChildren;i++) {
var mc = this.gamelevel.getChildAt(i);
if (mc is Coin) {
if ((forestLevel1Visited == true) && (currentLabel == "forestLevel1")) {
var p:int=0;
while(true) {
if (p > forestLvl1ObjectsDeletedX.length) {
break;
}else if ((mc.x == forestLvl1ObjectsDeletedX[p]) && (mc.y == forestLvl1ObjectsDeletedY[p])) {
gamelevel.removeChild(mc);
forestLvl1ObjectsDeletedX.splice(p,1);
forestLvl1ObjectsDeletedY.splice(p,1);
break;
}else {
treasures.push(mc);
gamelevel.mc.gotoAndPlay(Math.floor(Math.random()*21-1));
}
p++;
}
}
/*for (var z:int=0;z<=forestLvl1ObjectsDeleted.length;z++) {
if (mc.x == forestLvl1ObjectsDeleted[z]) {
gamelevel.removeChild(mc);
forestLvl1ObjectsDeleted.splice(z,1);
}
break;
else {
treasures.push(mc);
mc.gotoAndPlay(Math.floor(Math.random()*21-1));
}
break;
}
}*/
else {
treasures.push(mc);
mc.gotoAndPlay(Math.floor(Math.random()*21-1));
}
}
}
}
Thanks in advance and kind regards,
Zak
reformatted the code - Tony
Last edited by tonypa; 03-24-2008 at 04:15 AM.
-
Senior Member
I think there is problem with variable "p". You are comparing both x and y values with p and only if both x and y are equal and also equal to p is mc removed. Basically, only objects at x=y=p could be removed.
-
so basically what you are trying to do is create a game where you can go back in levels? But at the moment when you go back the objects that you have already picked up are still there? Right?
Well if that is right ^^^ then instead of using an array i personally would use a variable. So what you want to do is create a variable so when the heo touches say the coin then the var = 2 or whatever and then on the same frame you want to right a code like this:
PHP Code:
if (myVariable == 1){ coin._visible = false } else { coin._visible = true }
You would have to change the "coin" to whatever your instance names are for your objects.
-
Thanks a lot for the replies! The p-variable thing I'm 99% sure works, since when a coin is hit, I add both values to both X&Y arrays simultaneously so they would be in the same position. I'll definitely try out the other approach, though. The only bad thing then is I'll have to write instance names for all the coins ^^ (yes I'm lazy). Thanks a lot again
-
Senior Member
You probably right, I misunderstood the code. Sorry about the confusion.
-
That didn't work either! :@ It seems to work ok for the first 2 instances of objects, and then it goes bananas. Soooo pissed right now. Anyone else got an idea what might be wrong with this code? :/
-
Senior Member
Try this. Avoid dealing with coordinates like 23.00000000009 which are unpredictable:
PHP Code:
...else if ((Math.round(mc.x) == Math.round(forestLvl1ObjectsDeletedX[p])) && (Math.round(mc.y) == Math.round(forestLvl1ObjectsDeletedY[p]))) {
mc.visible = false;
break;
}
And in the checkCollisions function:
PHP Code:
if (hero.mc.hitTestObject(treasures[i]) && treasures[i].visible == true) {
addScore(1);
if (currentLabel == "forestLevel1") {
forestLvl1ObjectsDeletedX.push(Math.round(treasures[i].x));
forestLvl1ObjectsDeletedY.push(Math.round(treasures[i].y));
}
var pb:PointBurst = new PointBurst(gamelevel,1,treasures[i].x,treasures[i].y,10,50,20,0xFFFFFF);
treasures[i].visible=false;
}
-
Aha, thanks a lot! Will give it a try again now ^^
-
Bah, the code must hate me or something. Still refusing to work.. oh well:P Thanks a lot for the help though!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|