Hard Light Productions Forums
Off-Topic Discussion => Programming => Topic started by: Titan on January 18, 2011, 12:47:19 pm
-
Is there a way to addChild, and then set the rotation/angle to that of another object? I have an object that goes up along a trajectory, angling as it goes. When you click a second time, it stops, and addChild adds in a new object at the x/y coords, but I need it to also be at the same angle as the first object. Is there a way to do that?
I'm no good with programming, so sorry if it's hard to understand what I need. Here's the small bit of code I've gotten so far by guessing.
stage.addEventListener(MouseEvent.CLICK,beginlaunch)
var launched = false
function beginlaunch(even:MouseEvent) {
gamecatapultarm.play()
gamecatapultwinder.play()
gamecatapult.play()
gamecow.play()
var launched = true
}
var whee = new cowflingmotion
stage.addEventListener(MouseEvent.CLICK,launchcow)
function launchcow(even:MouseEvent) {
if (launched == true) {
gamecow.stop()
addChild(whee)
whee.x = gamecow.x
whee.y = gamecow.y
gamecow.hide
whee.play()
}
}
-
Whereas an educated guess is preferable to an uneducated one...
The API Reference (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/) is an invaluable asset. For this specific problem, I suggest seeing what properties of the DisplayObject class might be relevant.
-
OK, I have another problem, a rather gamebreaking one. It crashes.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at cowsNEW3_fla::MainTimeline/hitTest()
The problem is with the last function in the code, BUT I DON'T KNOW WHAT THE PROBLEM MEANS. It keeps referencing line 187, which is the one containing the removeChild of the tempMissile.
stage.addEventListener(MouseEvent.CLICK,beginlaunch);
var score:int = 0
output.text = String(score)
var benny = new gamesong()
var changame : SoundChannel;
changame = benny.play();
stage.addEventListener(Event.ENTER_FRAME, onframe)
var shiphit = new shipbox()
var launched = false;
var cowinair = false
var gamecow = new cowlaunch();
var stopalien = false
var failtimer = new Timer(15000)
var cowtimer = new Timer(400)
var cowtimerfinished = false
var ready = true
var forcefire = false
var makerespawnwork = true
var aim = new cowhit()
this.addChild(aim);
aim.startDrag(true,new Rectangle(0,0,1500,1500));
aim.alpha = 0
cowtimer.addEventListener(TimerEvent.TIMER, cowtimerevent)
function beginlaunch(event:MouseEvent) {
if (cowinair == false){
if (launched == false){
gamecatapultarm.play();
gamecatapultwinder.play() ;
gamecatapult.play();
stage.addChild(gamecow);
gamecow.x = 1493.6;
gamecow.y = 534.25;
gamecow.alpha = 100
gamecow.play();
var cowtimergo = true
launched = true;
cowtimer.start()
trace("begin launch sequence");
var ready = false
makerespawnwork = false
}
else{
trace("launch failure")
}
}
}
function cowtimerevent(e:TimerEvent){
cowtimerfinished = true
cowtimer.stop()
forcefire = true
}
stage.addEventListener(MouseEvent.CLICK,launchcow);
var tempMissile = new flingablecow
var removertimer = new Timer(4500)
function launchcow(event:MouseEvent) {
if (launched == true) {
if (cowinair == false){
if (cowtimerfinished == true) {
if (makerespawnwork == false) {
addChild(tempMissile)
gamecow.stop()
tempMissile.rotation = 45 + gamecatapultarm.arm.rotation
tempMissile.x = gamecow.x + gamecow.thecow.x
tempMissile.y = gamecow.y + gamecow.thecow.y
tempMissile.angle=Math.atan2 (aim.y-tempMissile.y,aim.x-tempMissile.x)
tempMissile.speed = 5
tempMissile.gotoAndPlay(2)
trace(tempMissile.angle);
removertimer.start()
removertimer.addEventListener(TimerEvent.TIMER, remover)
gamecow.alpha = 0
trace("FIRE AWAY!")
launched = false
cowinair = true
}
}
}
}
else{
trace("youfail")
}
}
stage.addEventListener(Event.ENTER_FRAME, cowfly)
function cowfly(event:Event) {
if (tempMissile.speed !=-1) {
tempMissile.x +=18*Math.cos(tempMissile.angle)
tempMissile.y +=18*Math.sin(tempMissile.angle)
}
}
removertimer.addEventListener(TimerEvent.TIMER, remover)
function remover(e:TimerEvent){
stage.removeChild(tempMissile)
removertimer.stop()
cowinair = false
stage.removeChild(gamecow)
makerespawnwork = true
}
var tempEnemy = new alienship()
tempEnemy.speed = -1
var timer = new Timer(100)
timer.start()
timer.addEventListener(TimerEvent.TIMER, addTester)
failtimer.addEventListener(TimerEvent.TIMER, gameover)
function gameover(e:TimerEvent){
gotoAndStop(1, "endscreen")
}
var enemyslope:Number;
function addTester(e:TimerEvent){
stage.addChild(tempEnemy)
trace("Enemy Enter")
tempEnemy.x= Math.random()*1000 + 100
tempEnemy.y= -200
tempEnemy.angle=Math.atan2 (475-tempEnemy.y,280-tempEnemy.x)
trace(tempEnemy.angle);
tempEnemy.speed=5;
timer.stop()
stage.addChild(shiphit)
shiphit.x = tempEnemy.x + 50
shiphit.y = tempEnemy.y + 50
shiphit.alpha=0
failtimer.reset()
failtimer.start()
}
function onframe(event:Event) {
if (tempEnemy.speed !=-1) {
tempEnemy.x +=5*Math.cos(tempEnemy.angle)
tempEnemy.y +=5*Math.sin(tempEnemy.angle)
shiphit.y +=5*Math.sin(tempEnemy.angle)
shiphit.x +=5*Math.cos(tempEnemy.angle)
}
}
failtimer.addEventListener(TimerEvent.TIMER, killalien)
function killalien(e:TimerEvent){
if (stopalien == true) {
stopalien = false
stage.removeChild(tempEnemy)
failtimer.stop()
stage.removeChild(shiphit)
}
}
addEventListener(Event.ENTER_FRAME, hitTest)
function hitTest(touch:Event):void
{
if (tempMissile != null)
{
if (shiphit.hitTestObject(tempMissile))
{
if(tempEnemy != null && !tempEnemy.dead && tempEnemy.stage != null)
{
trace("box hit")
stage.removeChild(tempEnemy)
tempEnemy.gotoAndStop(1)
stage.removeChild(tempMissile)
cowinair = false
trace("they hit")
stage.removeChild(gamecow)
removertimer.stop()
score = score+50
timer.start()
output.text = score.toString()
makerespawnwork = true
stage.removeChild(shiphit)
failtimer.stop()
stopalien = false
}
else
{
trace("hit, does not exist")
}
}
}
}
stop();
And no, my teacher can't help me. I'm significantly beyond them.
-
Here's something to think about:
When does tempMissile get assigned as a child of stage?
Is it before or after you try to remove it?
I'm sure your teacher can help, but try to remember the following:
It's not their job to fix the bugs for you. It's their job to help you learn how to find and fix the bugs yourself.
You wouldn't learn anything if the teacher just handed you a working program.