Copy link to clipboard
Copied
I am creating a game that a sheep need to jump or crouch to avoid obstacles. I had create 2 types of obstacles but they come in a constant speed. Sometimes the 2 obstacles come together make it impossible to avoid. Is there any way to change this? Can I make the 2 obstacles come in randomly in a random speed?Here is my code.
hole_mc.visible = false;
bird_mc.visible = false;
playhotarea_btn.addEventListener(MouseEvent.CLICK, removeInstructionBox);
function removeInstructionBox(event:MouseEvent😞void
{
playhotarea_btn.removeEventListener(MouseEvent.CLICK, removeInstructionBox);
instructionbox_mc.visible = false;
instructiontext_mc.visible = false;
playbtn_mc.visible = false;
playbtntext_mc.visible = false;
sheep_mc.sheepIN_mc.visible = false;
sheep_mc.gotoAndPlay("sheepwalk");
treebg_mc.gotoAndPlay("bgloop");
hole_mc.visible = true;
bird_mc.visible = true;
timer.start();
}
hole_mc.addEventListener(Event.ENTER_FRAME, holeMove);
function holeMove(event:Event😞void {
if (hole_mc.x>= -200) {
hole_mc.x -=7;
}else{
hole_mc.x=1080;
trace("hole loops");
}
}
bird_mc.addEventListener(Event.ENTER_FRAME, birdMove);
function birdMove(event:Event😞void {
if (bird_mc.x>= -200) {
bird_mc.x -=10;
}else{
bird_mc.x=1080;
trace("bird loops");
}
}
stage.addEventListener(Event.ENTER_FRAME, hitHole);
function hitHole(event:Event😞void{
if (sheep_mc.hitTestObject(hole_mc)){
gotoAndStop("GameOver");
hole_mc.removeEventListener(Event.ENTER_FRAME, holeMove);
stage.removeEventListener(Event.ENTER_FRAME, hitHole);
bird_mc.removeEventListener(Event.ENTER_FRAME, birdMove);
stage.removeEventListener(Event.ENTER_FRAME, hitBird);
}
}
stage.addEventListener(Event.ENTER_FRAME, hitBird);
function hitBird(event:Event😞void{
if (sheep_mc.hitTestObject(bird_mc)){
gotoAndStop("GameOver");
bird_mc.removeEventListener(Event.ENTER_FRAME, birdMove);
stage.removeEventListener(Event.ENTER_FRAME, hitBird);
hole_mc.removeEventListener(Event.ENTER_FRAME, holeMove);
stage.removeEventListener(Event.ENTER_FRAME, hitHole);
}
}
var currentSecond:Number = 0;
var delay:Number = 1000;
var timer:Timer = new Timer(delay);
timer.addEventListener(TimerEvent.TIMER, timerEventHandler);
function timerEventHandler(event:TimerEvent😞void
{
currentSecond++;
trace(currentSecond);
score_txt.text = String(currentSecond + " s");
}
Copy link to clipboard
Copied
If you want to have things happen with random numeric values then you need to make use of the Math.random() method. For instance, let's say you want the timer delay to be some random value that it is at least some minimum value but no more than some maximum value. The code that would be...
var delay:Number = minimumDelay + Math.random()*(maximumDelay - minimumDelay);
Copy link to clipboard
Copied
"Sometimes the 2 obstacles come together make it impossible to avoid. Is there any way to change this?"
change your code to sth like:
function chooseObstacle(e:TimerEvent):void
{
if (Math().random < .5)
{
init(hole_mc);
}
else
{
init(bird_mc);
}
}
function init(_target:MovieClip):void
{
_target.addEventListener(Event.ENTER_FRAME, move);
}
function move(e:Event):void {
//add here the code of the targets movement pattern,
//put in switch condition depending on the targets name/class
//when the obstacle has been reset (looped), remove the enterframeHandler
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now