Copy link to clipboard
Copied
so when evil dies it plays a death animation and is removed on the last frame
it works fine but an error show up
The supplied DisplayObject must be a child of the caller.
if(evil.currentFrame == 47)
{
if(contains(evil))
{
removeChild(evil);
}
if(contains(evilShadow))
{
removeChild(evilShadow);
}
}
Copy link to clipboard
Copied
if(contains(evil))
{
if(evil.currentFrame == 47)
{
removeChild(evil);
removeChild(evilShadow);
}
}
Copy link to clipboard
Copied
use:
if(evil.currentFrame == 47)
{
removeF(evil);
removeF(evilShadow);
}
function removeF(dobj:DisplayObject=null):void{
if(dobj&&dobj.stage){
dobj.parent.removeChild(dobj);
}
}
Copy link to clipboard
Copied
I am not sure but I think he's using the if statement on enter frame event.. so if he used if(evil.currentFrame == 47) first then he'll get an error after it has been removed..
Copy link to clipboard
Copied
i'm also pretty sure that code is within a loop (like enterfrmae), but he will still see an error with your code and not with my code. ie, removing a movieclip from the display doesn't change its frame or even whether its timeline continues to loop.
i believe he's getting that error because evil or evilShadow aren't children of 'this' when that error is triggered. ie, contains() returns true if the argument is a descendant of 'this' but it may not be a child of 'this'.
one solution is to use evil.parent.removeChild(evil) etc.
Copy link to clipboard
Copied
Actually I've tested the codes and it's all working well no errors even his code..
so I think that the "evil" object is not a child of "this" as you said.. you're right.
Copy link to clipboard
Copied
I changed some stuff.
instead of removing evil, it would respawn somewhere else and I just reset its coordinates
if(evil.currentFrame == 47)
{
var randomNum:Number = Math.random()*7
var roundedNum:Number = Math.floor(randomNum);
if(roundedNum == 0)
{
evilShadow.x = char.x+3000;
evilShadow.y = char.y+3000;
}
if(roundedNum == 1)
{
evilShadow.x = char.x+2000;
evilShadow.y = char.y;
}
if(roundedNum == 2)
{
evilShadow.x = char.x+3000;
evilShadow.y = char.y-3000;
}
if(roundedNum == 3)
{
evilShadow.x = char.x
evilShadow.y = char.y-2000;
}
if(roundedNum == 4)
{
evilShadow.x = char.x-3000;
evilShadow.y = char.y+3000;
}
if(roundedNum == 5)
{
evilShadow.x = char.x-3000;
evilShadow.y = char.y-3000;
}
if(roundedNum == 6)
{
evilShadow.x = char.x-2000;
evilShadow.y = char.y;
}
if(roundedNum == 7)
{
evilShadow.x = char.x;
evilShadow.y = char.y+2000;
}
evilHealth = 200;
evilDies = false;
died = false;
fadePresent = false;
darkhere = false;
if(contains(dark))
{
removeChild(dark);
}
I expected the last code to display the same error but it didn't, so I have no idea what causes the error.
Copy link to clipboard
Copied
nevermind an error showed up but its different
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/contains()
at HorrorGame/checkforHits()
this is the function
if(flashOn == false)
{
evilSpeed = 4;
}
if(flashOn == true)
{
if((evilShadow.x < char.x)&&(faceLeft == true))
{
if(char.hitTestObject(evilShadow))
{
evilHealth--;
evilSpeed = 0.5;
}
}
else if((evilShadow.x >= char.x)&&(faceRight == true))
{
if(char.hitTestObject(evilShadow))
{
evilSpeed = 0.5;
evilHealth--;
}
}
else if((evilShadow.y < char.y)&&(faceUp == true))
{
if(char.hitTestObject(evilShadow))
{
evilSpeed = 0.5;
evilHealth--;
}
}
else if((evilShadow.y > char.y)&&(faceDown == true))
{
if(char.hitTestObject(evilShadow))
{
evilSpeed = 0.5;
evilHealth--;
}
}
else
{
evilSpeed = 4;
}
}
if(evilShadow.hitTestObject(charShadow))
{
healthbar.x -= 1.2;
health -= 2;
}
else
{
}
healthbar.scaleX = health/500;
if(evilShadow.hitTestObject(charShadow))
{
if(fadePresent == false)
{
dark = new DarkFade;
dark.scaleX = 4;
dark.scaleY = 4;
addChild(dark);
this.setChildIndex(dark, this.numChildren-1);
fadePresent = true;
darkhere = true
}
}
if((evilShadow.hitTestObject(charShadow)==false)&&(darkhere == true))
{
fadePresent = false;
removeChild(dark);
darkhere = false;
}
if(evilHealth <= 0)
{
evilDies = true;
}
if(evil.currentFrame == 47)
{
var randomNum:Number = Math.random()*7
var roundedNum:Number = Math.floor(randomNum);
if(roundedNum == 0)
{
evilShadow.x = char.x+3000;
evilShadow.y = char.y+3000;
}
if(roundedNum == 1)
{
evilShadow.x = char.x+2000;
evilShadow.y = char.y;
}
if(roundedNum == 2)
{
evilShadow.x = char.x+3000;
evilShadow.y = char.y-3000;
}
if(roundedNum == 3)
{
evilShadow.x = char.x
evilShadow.y = char.y-2000;
}
if(roundedNum == 4)
{
evilShadow.x = char.x-3000;
evilShadow.y = char.y+3000;
}
if(roundedNum == 5)
{
evilShadow.x = char.x-3000;
evilShadow.y = char.y-3000;
}
if(roundedNum == 6)
{
evilShadow.x = char.x-2000;
evilShadow.y = char.y;
}
if(roundedNum == 7)
{
evilShadow.x = char.x;
evilShadow.y = char.y+2000;
}
evilHealth = 200;
evilDies = false;
died = false;
fadePresent = false;
darkhere = false;
if(contains(dark))
{
removeChild(dark);
}
}
Copy link to clipboard
Copied
and another thing I need help with
I have 10 trees and 10 shadows under the trees
i use the tree shadows for that if my character touches the shadow, he cant walk through the tree, but if he goes around, it would do the setChildIndex to go behind it or in front of it.
the problem is that it only takes the first shadow as the reference and if my char.y is greater than that certain shadow under the tree, then every single other tree would be behind my char
for(var treeNum:int = treeshadows.length-1;treeNum>=0;treeNum--)
{
for(var someTrees:int = trees.length-1;someTrees>=0;someTrees--)
{
if(char.hitTestObject(trees[someTrees]))
{
if(charShadow.y < treeshadows[treeNum].y)
{
this.setChildIndex(trees[someTrees], this.numChildren-1);
}
if(charShadow.y > treeshadows[treeNum].y)
{
this.setChildIndex(trees[someTrees], this.numChildren-4);
}
}
else
{
}
}
if(charShadow.hitTestObject(treeshadows[treeNum])&&(faceRight == true))
{
this.x += 0.3;
char.x -= 0.3;
healthbar.x -= 0.3;
batteryText.x -=0.3;
}
if(charShadow.hitTestObject(treeshadows[treeNum])&&(faceLeft == true))
{
this.x -= 0.3;
char.x += 0.3;
healthbar.x += 0.3;
batteryText.x += 0.3;
}
if(charShadow.hitTestObject(treeshadows[treeNum])&&(faceUp == true))
{
this.y -= 0.3;
char.y += 0.3;
healthbar.y +=0.3;
batteryText.y += 0.3;
}
if(charShadow.hitTestObject(treeshadows[treeNum])&&(faceDown == true))
{
this.y += 0.3;
char.y -= 0.3;
healthbar.y -= 0.3;
batteryText.y -=0.3;
}
}
Copy link to clipboard
Copied
ok I have no idea whats going on
just setting some stuff to make it simpler
I set it so there is only one tree. and that one tree is the only tree which that the depth works fine
my character depth is on top of the tree if his y cords are greater and is behind if char.y < than the tree shadow.
so "evil" is this ghost looking ghoul that follows you and to kill it, you shine ur flashlight at it.
when it 'dies' it explodes and it respawns somewhere far.
but if im behind a tree when I kill it, my char depth automatically switches to the top.
and did not remove any objects
the code for when evil dies should be somewhere up there
and the codes for setting the depth
Copy link to clipboard
Copied
you're still using the same problematic code.
Copy link to clipboard
Copied
I took out the remove child thingy, I don't get why moving evil to a different position and setting variables would affect the depth which is in a different function
Copy link to clipboard
Copied
i don't understand your question. but it may be that you do not understand 'scope'.
if you want to remove something from the display, you can always use the function i suggested. it eliminates the need to understand several things that can cause problems.
again, that function is:
function removeF(dobj:DisplayObject=null):void{
if(dobj&&dobj.stage){
dobj.parent.removeChild(dobj);
}
}
Copy link to clipboard
Copied
the question was about depth settings. i took out the removeChild(dark); so nothing is removed.
the game goes like this, the character shines the flash light at evil, and after a few seconds of flashing it it dissapears. originally, i removed it. now i just change its coordinates make it spawn else where. but when that happens. the depth changes. and my character depth is brought to the top which i don't want. codes are above
Copy link to clipboard
Copied
every time you use addChild() you add the argument object to the top of the display.
to prevent that you can use addChildAt() or, even easier to control, is to add a parent object (at the depth you want all children to appear) and then add the children to that parent.
Copy link to clipboard
Copied
but I didn't add anything new. there are three objects seen in the game when it starts, a tree, a character, and an evil ghoul. Depending on character's y coordinates, it will be either behind or in front of the tree. and the evil ghoul is just there. When I kill the ghoul, I don't remove anything, the ghoul plays its death animation and respawns some other place. but once I "kill' it, my char depth settings go to the top even though the same three objects are still in the scene
Copy link to clipboard
Copied
what do you mean by, '..respawns'?
that sounds like you're creating another new object and, if you do that, you have to add it to the display list for it to be visible.
Copy link to clipboard
Copied
i mean i just change the coordinates. as i said nothing is removed or added.
Copy link to clipboard
Copied
if you only change the x,y of an object (eg, obj) its depth will not change from that. but removing other objects can (and will) change obj's depth is obj is/was above the removed object.
Copy link to clipboard
Copied
yes so I don't get why this part of the function would change the depth
if(evilHealth <= 0)
{
evilDies = true;
}
if(evil.currentFrame == 47)
{
var randomNum:Number = Math.random()*7
var roundedNum:Number = Math.floor(randomNum);
if(roundedNum == 0)
{
evilShadow.x = char.x+3000;
evilShadow.y = char.y+3000;
}
if(roundedNum == 1)
{
evilShadow.x = char.x+2000;
evilShadow.y = char.y;
}
if(roundedNum == 2)
{
evilShadow.x = char.x+3000;
evilShadow.y = char.y-3000;
}
if(roundedNum == 3)
{
evilShadow.x = char.x
evilShadow.y = char.y-2000;
}
if(roundedNum == 4)
{
evilShadow.x = char.x-3000;
evilShadow.y = char.y+3000;
}
if(roundedNum == 5)
{
evilShadow.x = char.x-3000;
evilShadow.y = char.y-3000;
}
if(roundedNum == 6)
{
evilShadow.x = char.x-2000;
evilShadow.y = char.y;
}
if(roundedNum == 7)
{
evilShadow.x = char.x;
evilShadow.y = char.y+2000;
}
evilHealth = 200;
evilDies = false;
died = false;
fadePresent = false;
darkhere = false;
I just reset variables back to default just like it would be in the start of the game
and I change evilShadow's coordinates
Copy link to clipboard
Copied
and meanwhile I was working on another thing for another game something
public function animateHero(event:Event)
{
if(MovieClip(parent).faceRight1 == true)
{
this.scaleX = 0.5;
}
if(MovieClip(parent).faceLeft1 == true)
{
this.scaleX = -0.5;
}
if(MovieClip(parent).moving1 == false)
{
this.gotoAndStop(1);
}
if(MovieClip(parent).moving1 == true)
{
if(this.currentFrame == 1)
{
this.gotoAndPlay(2);
}
if(this.currentFrame == 9)
{
this.gotoAndPlay(5);
}
}
}
on this last part, frame 5 is played in two frames before playing on
I don't get why that happens.
I thought it would go
1 2 3 4 5 6 7 8 (9)
5 6 7 8 (9) and then
5 6 7 8 (9) and so on
but instead it goes
1 2 2 3 4 5 6 7 8 (9)
5 5 6 7 8 (9)
5 5 6 7 8 (9) and so on
frame two is also repeated twice for some reason
I saw this by putting fps to 1
Copy link to clipboard
Copied
use the trace function to debug your code.
Copy link to clipboard
Copied
yes but my Character depth changes
Copy link to clipboard
Copied
this is the code that changes the depth of char. I move the tree depth front and back depending on charShadow.y
after the evil death thing, charShadow doesn't change but char depth jumps to the top
for(var treeNum:int = treeshadows.length-1;treeNum>=0;treeNum--)
{
for(var someTrees:int = trees.length-1;someTrees>=0;someTrees--)
{
if(char.hitTestObject(trees[someTrees]))
{
if(charShadow.y < treeshadows[treeNum].y)
{
this.setChildIndex(trees[someTrees], this.numChildren-1);
}
if(charShadow.y > treeshadows[treeNum].y)
{
this.setChildIndex(trees[someTrees], this.numChildren-4);
}
}
else
{
}
}
Copy link to clipboard
Copied
again, use the trace function to debug your code.
it would be significant work for me to debug your code for you. i can do that (if i'm hired), but you should be able to solve this yourself using the trace function.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now