-
-
Use a mouseMove or enterFrame on the stage so you can run a function every time the player is moved. When that happens, use a hitTest to see if the player has hit the power up - and if so just move the powerup to a new position (stageWidth * math.random, stageHeight * math.random) - then add one to the score.
-
Thanks for the help.
Yah, that part I knew, but I'm a bit confused with the code iteself because in this game, I'm placing all the code in frames.
-
Moving across frames shouldn't be an issue unless objects are being destroyed and recreated or you're running code at different times - is there something specific that's breaking? Can you post some code?
-
http://pat.theorigin.net
place all of your code in frame one. if you want the code to execute at some time other than frame one, put it in a function on frame one and make a call to the function.
-
Thanks for the help.
Maybe I wasn't clear. I meant I´m not using class files (I know I should), therefore I'm placing the code on frames, which makes the code a little bit different.
I have been learning to code with .as files but for this project I wanted to try to code using the frames.
This is what I have on my "play" frame. There's a "var mainScore:int = 0;" but I haven't implemented it yet.
PHP Code:
stop();
// VARIABLES
var mainScore:int = 0;
var ballXSpeed:Number = -8; var ballYSpeed:Number = -8;
var starXSpeed:Number = 8; var starYSpeed:Number = 8;
var leftKeyDown:Boolean = false; var upKeyDown:Boolean = false; var rightKeyDown:Boolean = false; var downKeyDown:Boolean = false;
var manSpeed:Number = 7;
// MOVE RED CIRCLE
mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
function moveBall(event:Event):void { mcBall.x += ballXSpeed; mcBall.y += ballYSpeed; if(mcBall.x >= stage.stageWidth-mcBall.width) { ballXSpeed *= -1; } if(mcBall.x < 0) { ballXSpeed *= -1; } if(mcBall.y >= stage.stageHeight-mcBall.height) { ballYSpeed *= -1; } if(mcBall.y <0) { ballYSpeed *= -1; } }
// MOVE STAR
mcStar.addEventListener(Event.ENTER_FRAME, moveStar);
function moveStar(event:Event):void { mcStar.x += starXSpeed; mcStar.y += starYSpeed; if(mcStar.x >= stage.stageWidth-mcStar.width) { starXSpeed *= -1; } if(mcStar.x < 0) { starXSpeed *= -1; } if(mcStar.y >= stage.stageHeight-mcStar.height) { starYSpeed *= -1; } if(mcStar.y <0) { starYSpeed *= -1; } }
man.addEventListener(Event.ENTER_FRAME, moveMan)
// MOVE PLAYER
function moveMan(event:Event):void { Mouse.hide(); man.x = mouseX; man.y = mouseY; }
// COLLISION CIRLE AND MAN
stage.addEventListener(Event.ENTER_FRAME, circleHits);
function circleHits(event:Event):void { if(mcBall.hitTestObject(man)) { man.removeEventListener(Event.ENTER_FRAME, moveMan) stage.removeEventListener(Event.ENTER_FRAME, circleHits); mcStar.removeEventListener(Event.ENTER_FRAME, moveStar); mcBall.removeEventListener(Event.ENTER_FRAME, moveBall); gotoAndStop("gameOver"); } }
Maybe I should do two functions. One for the collision and one for the power up creation and placement? What would you do?
Thanks again.
Last edited by Hurdarr; 05-30-2009 at 09:06 PM.
-
http://pat.theorigin.net
There is really very little difference using code in frames and code in classes. What is the problem with that code?
-
Yes, but there some subtle differences.
The collision part is done but I'm having some problems with the second, creating a function that does (when the player hits the power up):
1) The power-up to be removed;
2) Increase the score by 1.
3) Place a new power up randomly on the stage.
4) Rinse and repeat.
-
You should be able to just drop this into your moveMan function:
PHP Code:
if(mcStar.hitTestObject(man)){
mainScore++;
while(mcStar.hitTestObject(man)){
mcStar.x = Math.random() * stage.stageWidth;
mcStar.y = Math.random() * stage.stageHeight;
}
}
-
Edit:Nevermind, I was coding like I was using classes.
Thanks for the help
Another quick question.
Everytime the player collides with the star, I want the ball to move gradually faster.
PHP Code:
function moveMan(event:Event):void { Mouse.hide(); man.x = mouseX; man.y = mouseY; if(mcStar.hitTestObject(man)){ while(mcStar.hitTestObject(man)) { mcStar.x = Math.random() * stage.stageWidth; mcStar.y = Math.random() * stage.stageHeight; gameScore++; gameScoreField.text = String(gameScore); ballXSpeed += 1; ballYSpeed += 1; } } }
Oddly, sometimes the ball moves slower. Anyone knows why?
When I increase the values from 1 to 5, the ball does go faster sometimes (and slows) but also does some weird twists.
Finally, in addition of making the ball gradually faster, I also and to make it gradually bigger. Which property do I use? Scale?
Thanks.
Last edited by Hurdarr; 05-31-2009 at 06:14 PM.
-
Hrmm - your ball will slow down if it's already moving in a negative direction (eg. - if speed is -7, adding 1 gives you -6) - you could use a conditional to test:
PHP Code:
ballXSpeed += (ballXSpeed > 1) ? 1 : -1;
And yeah - you can just add to scaleX and scaleY to make the ball bigger (you could just use *= 1.25 for +25%).
-
http://pat.theorigin.net
you could also store velocity as angle and speed rather than x and y speeds. That way when you add 1 to speed it always speeds up without the conditional.
while this does get into some trig, it also makes the physics of your game much more straightforward.
-
Thanks for the input guys.
Hrmm - your ball will slow down if it's already moving in a negative direction (eg. - if speed is -7, adding 1 gives you -6)
I spent almost an hour trying to find out why the ball moved slower. I totally forgot I had negative speed. Of course the ball goes slower when the value approaches 0.
Mental note: don't code when you're tired.
Hmm...that conditional code, that's a ternary operator right? I must do a bit of research first, that's new stuff for me.
Thanks for the help.
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
|