actionscript 3: changing object size after a hitTestObject - actionscript-3

I want to make my players become bigger or smaller in diameter when they eat a good(theBall2) or bad ball(theBadBall) respectively. ive created a hitTestObject function where I tried to make the ball get smaller and bigger but it doesn’t work.
var player1;
var player2;
var player3;
var theBall2;
var theBadBall;
player1 = new player(50,384, 1);
player2 = new player(944,384,2);
player3 = new player(488,84,3);
stage.addChild(player1);
stage.addChild(player2);
stage.addChild(player3);
var player.width: player.width * 2; // 1086 syntax error: expecting semicolon before dot.
var player.height:player.height * 2;// 1086 syntax error: expecting semicolon before dot.
//this function checks to see if theBall2 has collided with a player
if(theBall2.hitTestObject(player1) || theBall2.hitTestObject(player2) || theBall2.hitTestObject(player3))
{
//removes the ball from the stage
trace("a player has eaten a ball");
stage.removeChild(theBall2);
//adds new ball
theBall2 = new ball2();
theBall2.x = stage.stageWidth/2;
theBall2.y = stage.stageHeight/2;
stage.addChild( theBall2 );
//makes player larger
player.width = player.width * 4; // do I have to define player1, player2, player3, instead of their class player?
player.height = player.height * 4;
}
//this function checks to see if theBadBall has collided with a player
if(theBadBall.hitTestObject(player1) || theBadBall.hitTestObject(player2) || theBadBall.hitTestObject(player3))
{
//removes thebadball from the stage
trace("a player has eaten bad ball");
stage.removeChild(theBadBall);
//adds new ball
theBadBall = new badball();
theBadBall.x = stage.stageWidth/2;
theBadBall.y = stage.stageHeight/2;
stage.addChild(theBadBall);
//makes player smaller
player.width = player.width / 2;
player.height = player.height / 2;
}
How do I make it work?

There's no player anywhere, so you can't change its height or width.
And these lines are completely wrong: var player.width: player.width * 2;.
You have player1, player2 and player3 and you need to change their sizes. Those are MovieClips?
If so, you could for example:
var player:MovieClip;
var goodBallHit:Boolean;
if(theBall2.hitTestObject(player1)){
player = player1;
goodBallHit = true;
}else if(theBall2.hitTestObject(player2)){
player = player2;
goodBallHit = true;
}else if(theBall2.hitTestObject(player3)){
player = player3;
goodBallHit = true;
}
if(goodBallHit){
stage.removeChild(theBall2);
theBall2 = new ball2();
theBall2.x = stage.stageWidth/2;
theBall2.y = stage.stageHeight/2;
stage.addChild(theBall2);
player.width = player.width * 4;
player.height = player.height * 4;
}
So you really need to refer the actual object, player1, ..2 or ..3 at this case.
This is only one way, and there's many others too. I wouldn't recommend using hitTestObject at all, especially with balls, but this might be a good method for you to start with.
Your whole project would need a bit more planning, but I understand it's pretty hard if you don't know the language too well. Just have some patience, SEARCH info from this site & google, and you'll get this done suprisingly fast I'm sure.
(+You really need to continue googling, there must be dozens of similar game sources and tutorials out there)

Related

How to make my player die when it touch enemy (die when other object is touching it)?

So i made my first game on Adobe Animate CC (using AS 3.0). The concept of the game is a zombie who haunted & surrounding by living creature (people), so if the people is touch the zombie (my player), the zombie will die. Also, the people/enemy will move randomly in the area.
Currently, my progress so far is done with making the zombie movement (also, for the movement is like you play DOTA 2. You click your mouse on specific area, and the zombie would be go to that specific area too).
Here's my code so far:
import flash.events.MouseEvent;
import flash.events.Event;
crewMC.stop();
var moveX:Number = crewMC.x;
var moveY:Number = crewMC.y;
crewMC.addEventListener(Event.ENTER_FRAME, moveCrew);
stage.addEventListener(MouseEvent.CLICK, clickArea);
function clickArea(e:MouseEvent):void{
moveX = mouseX;
moveY = mouseY;
}
function moveCrew(e:Event):void{
var ob:Object = e.currentTarget;
var distX:Number = moveX-ob.x;
var distY:Number = moveY-ob.y;
var dist:Number = Math.sqrt(distX*distX+distY*distY);
if (dist < 10){
//iddle
ob.gotoAndStop(1);
}else{
//run
ob.gotoAndStop(2);
var rad:Number = Math.atan(distY/distX);
if (distX>=0){
ob.scaleX = 1;
ob.rot = rad*180/Math.PI;
} else {
ob.scaleX = -1;
ob.rot = rad*180/Math.PI+180;
}
ob.x += 5 * Math.cos(ob.rot*Math.PI/180);
ob.y += 5 * Math.sin(ob.rot*Math.PI/180);
}
}
So can you guys please help me? My struggle is:
How can I add the people/enemy move randomly on the area?
How can I add death effect when the zombie is touched by enemy?
Thank you guys! Every response from you is really appreciate!

ActionScript 3 How do I "move" coins when a player movieclip touches it

So let's say I have a circle object that I can move around in an open field, essentially I want to know if its possible to move the coin to a random spot, giving it an illusion of collecting a coin and spawning a new one.
However I can't seem to move it when my object symbol touches the coin, can I get some help?
var randomX:Number = Math.random() * stage.stageWidth
var randomY:Number = Math.random() * stage.stageHeight
var coin:item_point = new item_point;
coin.x = randomX;
coin.y = randomY;
addChild(coin);
addEventListener(Event.ENTER_FRAME, interaction);
function interaction(event:Event):void
{
if (mc_circle.hitTestObject(coin)){
total += 100;
coin.x = randomX;
coin.y = randomY;
}

HitTestObject Both MovieClip Actionscript 3.0

I want to make a player and 2 Circles.
When the player hit the first Circle, then the Circle will also move as like player. Continue with the second Circle, if the second circle get hit by the first circle (while the player is moving and pushing the first circle), the second circle will also move as like player's speed movement!
Can you solve the problems, please ... :)
Thank You!
Use the addChild() method.
var circle1Hit:Boolean = false;
var circle2Hit:Boolean = false;
function myHitTest(me:MouseEvent): void
{
if (player.hitTestObject(circle1) && circle1Hit == false){
circle1Hit = true;
var _x:Number = circle1.x - player.x;
var _y:Number = circle1.y - player.y;
player.addChild(circle1)
circle1.x = _x;
circle1.y = _y;
}
if (player.hitTestObject(circle2) && circle2Hit == false)
{
circle2Hit = true;
var _x:Number = circle2.x - player.x;
var _y:Number = circle2.y - player.y;
player.addChild(circle2)
circle2.x = _x;
circle2.y = _y;
}
}
For further reading check out this great tutorial that explains containers and OOP really well. Also check out the one on Arrays from the same author here. By using an array, you could add even more circles to the array and all of them would be able to stick like these two do without having to have a separate bit of code for each circle like we have here.

Actionscript 3 game: object respawning

Im trying to make a simple game in AS3 where a player eats as many balls as possible. i dont know how to code very well and im having trouble trying to add a new ball to the stage every time one is eaten.
this is the code i have in main.as at the moment.
private var startX:Number = 512;
private var startY:Number = 384;
private var speed:Number = 8;
var player1;
var player2;
var player3;
var theBall;
player1 = new player(50,384, 1);
player2 = new player(944,384,2);
player3 = new player(488,84,3);
stage.addChild(player1);
stage.addChild(player2);
stage.addChild(player3);
if(theBall.hitTestObject(player1) || theBall.hitTestObject(player2) || theBall.hitTestObject(player3))
{
//removes the ball from the stage
trace("a player has eaten a ball");
stage.removeChild(theBall);
//adds new ball
//stage.addChild(theBall);
//reset x and y
startX = Math.random()*speed-speed/2;
startY = Math.random()*speed-speed/2;
}
In the ball.as ive specified how the ball should move randomly, start in the center of the stage and bounce off walls.
no errors appear, the code just doesnt work. how do you make a new ball re spawn in the center of the stage when one is eaten? do i declare this where i tried to in the main, or in the ball.as?
If you have a "ball" Object, in the way you have a "player" Object, then...
//removes the ball from the stage
trace("a player has eaten a ball");
stage.removeChild(theBall);
//reset x and y
startX = Math.random()*speed-speed/2;
startY = Math.random()*speed-speed/2;
//adds new ball
theBall = new ball();
theBall.x = stage.stageWidth/2;
theBall.y = stage.stageHeight/2;
stage.addChild( theBall );
You reuse the "theBall" variable, since you've removed the eaten instance from the stage. The old "theBall" instance is thrown out with stage.removeChild(theBall), and then you make a new one and add that new one to the stage.
Before your code, if the BALL isn't called and prepared for your stage. It might not work.
In your Library, check the movie_clip that contain the ball... And under the row "AS LINKAGE" next to the name of the Movie Clipe... You should call it for example "BALL_TEST"
In your AS3 code :
Add this before your code
var ball_bounce:BALL_TEST = new BALL_TEST();
Then your code
If i did correctly understand your question...
Kind regards

Add child + hittest?

AS3 Flash -
Hey, I made a game like "Flappy Bird".
Can someone help me how to check "character" hitted a wall?
This game generate every 2.5 seconds two walls. (Wall and Wall2)
How to check that character hitted it?
function newWall():void
{
var Wall:wall = new wall();
addChildAt(Wall, 1);
Wall.x = -350 + (-80 - -350) * Math.random();
Wall.y = 805;
Wall.name = "Wall1_Object";
TweenLite.to(Wall, 10, {y:-50, ease:Linear.easeNone});
var Wall2:wall = new wall();
addChildAt(Wall2, 1);
Wall2.x = Wall.x + Wall.width + 125;
Wall2.y = 805;
Wall2.name = "Wall2_Object";
TweenLite.to(Wall2, 10, {y:-50, ease:Linear.easeNone});
}
function checkDead():void
{
if (character.hitTestObject(Wall) || character.hitTestObject(Wall2))
{
trace("You dead!");
}
}
var newWallInterval:uint = setInterval(newWall,2500);
var checkDeadInterval:uint = setInterval(checkDead,500);
If the last walls can't be hit any more when the new walls created,Try to define Wall and Wall2 outside the new newWall function.,or you need create an array to save the walls, and do a loop to check if the role hit the wall.