gpt4 book ai didi

actionscript-3 - 从Assets类外部初始化八哥的声音

转载 作者:行者123 更新时间:2023-12-03 02:10:02 27 4
gpt4 key购买 nike

我正在lynda.com上关注一个教程,其中详细介绍了如何为八哥游戏添加声音(如果您有Lynda,则由Lee Brimelow制作)。在教程中,他像这样将声音嵌入Assets.as类中

    [Embed(source="../media/sounds/laser-shot-silenced.mp3")]
private static var LaserSound:Class;
public static var LaserSFX:Sound;

[Embed(source="../media/sounds/mariah-carey-dubstep.mp3")]
private static var Track1:Class;
public static var Track1SFX:Sound;

然后使用init函数初始化它们,该函数位于Assets.as类的下方。看起来像这样...
    public static function init():void
{
skyTexture = Texture.fromBitmap(new sky());

ta = new TextureAtlas(Texture.fromBitmap(new atlas()),
XML(new atlasXML()));

TextField.registerBitmapFont(new BitmapFont(Texture.fromBitmap(new komaka()),
XML(new komikaXML()));

LaserSFX = new LaserSound();
Track1SFX = new Track1();
}

我的问题是我的init函数位于InGame.as类中,如果我尝试在那里初始化SFX,例如
    public function initialize():void
{
LaserSFX = new LaserSound();
Track1SFX = new Track1();

它们显示为未定义的属性。我认为这可以通过添加来解决
import Assets;

到我的InGame.as类的顶部,但这似乎无济于事。

我更具体的问题是如何在Assets类中初始化这些声音,以及如何初始化它们?另外,我有一个专门针对Laser Shot的类(class),是不是应该在该类(class)中初始化,还是应该在我的InGame类(class)中?

很抱歉,如果这个问题的整理不当,请告诉我该怎么办才能解决,如果您无法理解,请提供更多信息。

在下面,我将复制并粘贴正在使用的文件...

InGame.as

包装画面
{
导入Box2D.Common.Math.b2Vec2;
import com.reyco1.physinjector.PhysInjector;
import com.reyco1.physinjector.data.PhysicsObject;
import com.reyco1.physinjector.data.PhysicsProperties;

import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.Dictionary;

import objects.Enemy;
import objects.GameBackground;
import objects.Laser;
import objects.Scarlet;
import objects.SnowBall;

import Assets;

import starling.core.Starling;
import starling.display.Button;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
import starling.utils.deg2rad;

public class InGame extends Sprite
{
private var startBtn:Button;
private var replayBtn:Button;
private var gameOver:Image;
private var bg:GameBackground;
private var scarlet:Scarlet;

private var enemies:Array;
private var lasers:Array;
private var presents:Array;

private var lasersLayer:Sprite;
private var enemiesLayer:Sprite;

private var enemySpawnDelay:Number;
private var enemySpawnCounter:Number;

private var difficulty:Number;
private var difficultyRate:Number;

private var gameState:String;
private var playerSpeed:Number;
private const MIN_SPEED:Number = 650;

private var gameArea:Rectangle;
private var touchArea:Rectangle;
private var shape:starling.display.Sprite = new starling.display.Sprite();

private var target:Point = new Point(100, 100);

private var touch:Touch;
private var touchX:Number;
private var touchY:Number;

protected var sack:Image;
protected var physics:PhysInjector;

private var paused:Boolean = true;

public function InGame()
{
super();
this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
drawGame();
}

private function drawGame():void
{
presents = new Array();

bg = new GameBackground();
this.addChild(bg);

gameOver = new Image(Assets.getTexture("GameOver"));
gameOver.x = 83;
gameOver.y = 130;
this.addChild(gameOver);

scarlet = new Scarlet;
scarlet.x = stage.stageWidth/2;
scarlet.y = stage.stageHeight/2;
this.addChild(scarlet);
setChildIndex(scarlet, 2)

startBtn = new Button(Assets.getTexture("PlayBtn"));
startBtn.x = stage.stageWidth * 0.5 - startBtn.width * 0.5;
startBtn.y = stage.stageHeight * 0.5 - startBtn.height * 0.5 + 35;
this.addChild(startBtn);

replayBtn = new Button(Assets.getTexture("ReplayBtn"));
replayBtn.x = 393;
replayBtn.y = 287;
this.addChild(replayBtn);

//defines area scarlet can fly in
gameArea = new Rectangle(0, 15, stage.stageWidth, stage.stageHeight - 150);

PhysInjector.STARLING = true;
physics = new PhysInjector(Starling.current.nativeStage, new b2Vec2(0, 40), true);

addEventListener(Event.ENTER_FRAME, updatePhysics);

addSack();
addPresents();
}

private function addSack():void
{
sack = new Image(Assets.getTexture("Sack"));
sack.x = 0;
sack.y = 510;
addChild(sack);

physics.injectPhysics(sack, PhysInjector.SQUARE, new PhysicsProperties({isDynamic:false, friction:0.5, restitution:0.5}));
}

private function addPresents():void
{
for(var i:int = 0; i < 10; i++)
{
var present:Image = new Image(Assets.getTexture("Present01"));
//present.x = 0;
present.y = 434;

if(i < 4)
present.x = i * present.width;
else if (i < 7 && i > 3)
{
present.x = (i-3.5) * present.width;
present.y = present.height * 4.5;
}
else if (i >= 7 && i < 9)
{
present.x = (i-6) * present.width;
present.y = present.height * 3.5;
}
else if (i > 8)
{
present.x = (i-7.5) * present.width;
present.y = present.height * 2.5;
}

addChild(present);
setChildIndex(present, 1);
presents.push(present);

var presentP:PhysicsObject = physics.injectPhysics(present, PhysInjector.SQUARE, new PhysicsProperties({isDynamic:true, isDraggable:false, friction:0.2, restitution:0.5}));
}
}

public function disposeTemporarily():void
{
this.visible = false;
}

public function initialize():void
{
//LaserSFX = new LaserSound();
//Track1SFX = new Track1();

this.visible = true;

scarlet.x = -stage.stageWidth;
scarlet.y = stage.stageHeight * 0.5;

gameState = "idle";

playerSpeed = 0;

difficultyRate = 0.3;
difficulty = 1;
enemySpawnDelay = enemySpawnCounter = 100;
enemySpawnCounter = 100;

enemies = new Array();
lasers = new Array();

addEventListener(Event.ENTER_FRAME, update);

lasersLayer = new Sprite();
enemiesLayer = new Sprite();

addChild(lasersLayer);
addChild(enemiesLayer);

replayBtn.visible = false;
gameOver.visible = false;

startBtn.addEventListener(Event.TRIGGERED, onStartBtnClick);
stage.addEventListener(TouchEvent.TOUCH, shootLaser);
replayBtn.addEventListener(Event.TRIGGERED, onReplayBtnClick);

this.addEventListener(TouchEvent.TOUCH, onTouch);
}

private function onStartBtnClick(event:Event):void
{
startBtn.visible = false;
startBtn.removeEventListener(Event.TRIGGERED, onStartBtnClick);

launchScarlet();
enemies.length = 0;
makeEnemies();

paused = false;
}

private function onReplayBtnClick(event:Event):void
{
replayBtn.visible = false;
replayBtn.removeEventListener(Event.TRIGGERED, onReplayBtnClick);

launchScarlet();
enemies.length = 0;
makeEnemies();

paused = false;
}

private function launchScarlet():void
{
this.addEventListener(TouchEvent.TOUCH, onTouch);
this.addEventListener(Event.ENTER_FRAME, onGameTick);
}

private function onTouch(event:TouchEvent):void
{
touch = event.getTouch(stage);
if(touch){
touchX = touch.globalX;
touchY = touch.globalY;

target.x = event.touches[0].globalX;
target.y = event.touches[0].globalY;
}

}

private function onGameTick(event:Event):void
{
switch(gameState)
{
case"idle":
//Take off
if (scarlet.x < stage.stageWidth * 0.5 * 0.5)
{
scarlet.x += ((stage.stageWidth * 0.5 * 0.5 + 10) - scarlet.x) * 0.5;
scarlet.y = stage.stageHeight * 0.5;

playerSpeed += (MIN_SPEED - playerSpeed) * 0.05;
}
else
{
gameState = "flying";
}
break;
case"flying":
playerSpeed -= (playerSpeed - MIN_SPEED) * 0.01;

scarlet.y -= (scarlet.y - touchY) * 0.4;

if (-(scarlet.y - touchY) < 150 && -(scarlet.y - touchY) > -150)
{
scarlet.rotation = deg2rad(-(scarlet.y - touchY) * 0.075);
}

if (scarlet.y > gameArea.bottom - scarlet.height * 0.5)
{
scarlet.y = gameArea.bottom - scarlet.height * 0.5;
scarlet.rotation = deg2rad(0);
}
if (scarlet.y < gameArea.top + scarlet.height * 0.5)
{
scarlet.y = gameArea.top + scarlet.height * 0.5;
scarlet.rotation = deg2rad(0);
}
break;
case"over":

paused = true;

scarlet.visible = false;
replayBtn.visible = true;
gameOver.visible = true;

//if(presents.length == 0)
// trace("your a loser");

break;
}
}

//addapted from "Shoot"

private function shootLaser(event:TouchEvent):void
{
touch = event.getTouch(this, TouchPhase.ENDED);
if(touch){

var newLaser:Laser = new Laser();

newLaser.x = scarlet.x - 35;
newLaser.y = scarlet.y - 20;

newLaser.xVel = 50;

newLaser.addEventListener(Laser.PURGE_EVENT, purgeLaserHandler);

lasersLayer.addChild(newLaser);
lasers.push(newLaser);
}
}


private function purgeLaserHandler(event:Event):void
{
var targetLaser:Laser = Laser(event.target);
purgeLaser(targetLaser);
}

private function purgeLaser(targetLaser:Laser):void
{
targetLaser.removeEventListener(Laser.PURGE_EVENT, purgeLaserHandler);

try
{
var i:int;
for (i = 0; i < lasers.length; i++)
{
if (lasers[i].name == targetLaser.name)
{
lasers.splice(i, 1);
lasersLayer.removeChild(targetLaser);
i = lasers.length;
}
}
}
catch(e:Error)
{
trace("Failed to delete enemy!", e);
}
}

private function makeEnemies():void
{
enemySpawnCounter++;

if (enemySpawnCounter > enemySpawnDelay)
{
enemySpawnCounter = 0;
enemySpawnDelay -= difficultyRate/5;
difficulty += difficultyRate/5;
makeEnemy();
}
else if (enemySpawnCounter <= enemySpawnDelay)
{
enemySpawnCounter == enemySpawnDelay
}
}

private function makeEnemy():void
{
var i:int;

for (i = 0; i < Math.floor(difficulty); i++)
{
var newEnemy:Enemy = new SnowBall();

newEnemy.x = 925;
newEnemy.y = Math.random() * 375 + 50;
newEnemy.xVel = (-Math.random() * difficulty) - 5;
newEnemy.sinMeter = Math.random() * 10;
newEnemy.bobValue = Math.random() * difficulty;

newEnemy.addEventListener(Enemy.PURGE_EVENT, purgeEnemyHandler);

enemiesLayer.addChild(newEnemy);
enemies.push(newEnemy);

//To make an enemy with gravity
//var enemyP:PhysicsObject = physics.injectPhysics(newEnemy, PhysInjector.CIRCLE, new PhysicsProperties({density:1.5, friction:0.2, restitution:0.5}));
//enemyP.physicsProperties.linearVelocity = new b2Vec2(-20, -8);
}
}

private function purgeEnemyHandler(event:Event):void
{
var targetSnowBall:Enemy = Enemy(event.target);
purgeSnowBall(targetSnowBall);
}

private function purgeSnowBall(targetSnowBall:Enemy):void
{
targetSnowBall.removeEventListener(Enemy.PURGE_EVENT, purgeEnemyHandler);

try
{
var i:int;
for (i = 0; i < enemies.length; i++)
{
if (enemies[i].bobValue == targetSnowBall.bobValue)
{
enemies.splice(i, 1);
//enemiesLayer.removeChild(targetSnowBall);
targetSnowBall.visible = false; // Quick Fix
i = enemies.length;
}
}
}
catch(e:Error)
{
trace("Failed to delete enemy!", e);
}
}

private function laserHitTest(laser:Laser):void
{
for each (var enemy:Enemy in enemies)
{
if (enemy.status != "Dead" && enemy.bounds.intersects(laser.bounds))
{
enemy.status == "Dead";
enemy.destroy();
purgeSnowBall(enemy);
}
}
}

private function presentHitTest(presentP:Image):void
{
var i:int = 0;
for each (var enemy:Enemy in enemies)
{
if (enemy.status != "Dead" && enemy.bounds.intersects(presentP.bounds))
{
enemy.status == "Dead";
enemy.destroy();
purgeSnowBall(enemy);

//purgePresents();
//var presentPhys:PhysicsObject = physics.injectPhysics(presentP, PhysInjector.SQUARE, new PhysicsProperties({isDynamic:true, friction:0.2, restitution:0.5}));

clearPhysics(presentP);
presentP.visible = false; //works but is only a cheap hack


}i++;
}
}

private function purgePresents():void
{
// present.removeEventListener(Laser.PURGE_EVENT, purgeLaserHandler);
var i:int = 0;
for each (var present:Image in presents)
{
if (present.x < -10)
{
clearPhysics(present);
//removeChild(present, true); //Causes Crash in DynamicRegistration.changePropertyOnRegistrationPoint because no parent class
present.visible = false; //Quick fix
presents = presents.splice(i, 1);

// 1. Clear current present of physics
// 2. Make current present invisible (or remove from stage)
// 3. Remove current present from presents array
}
else if(present.y > stage.stageHeight)
{
clearPhysics(present);
//removeChild(present, true);
present.visible = false;
presents = presents.splice(i, 1);
}
i++;
}
}

private function updatePhysics():void
{
physics.update();
}

public function clearPhysics(present:Object):void
{
//removeEventListener(Event.ENTER_FRAME, updatePhysics);
if(physics.bodies.indexOf(present) != -1)
physics.removePhysics(present);
trace("removing physi");
}

private function update():void
{
trace(presents.length);


for each (var enemy:Enemy in enemies)
{
enemy.update();
}

for each (var present:Image in presents)
{
presentHitTest(present);
}

purgePresents();

for each (var laser:Laser in lasers)
{
laser.update();
laserHitTest(laser);
}

if (enemies.length > 15)
{
gameState = "over";
}
if(!paused)
makeEnemies();
}
//addapted from "Shoot"
}

}

Assets


{
import flash.display.Bitmap;
import flash.media.Sound;
import flash.utils.Dictionary;

import starling.textures.Texture;
import starling.textures.TextureAtlas;

public class Assets
{
[Embed(source="../media/sounds/laser-shot-silenced.mp3")]
private static var LaserSound:Class;
public static var LaserSFX:Sound;

[Embed(source="../media/sounds/mariah-carey-dubstep.mp3")]
private static var Track1:Class;
public static var Track1SFX:Sound;

[Embed(source="../media/graphics/bgWelcome.png")]
public static const BgWelcome:Class;

[Embed(source="../media/graphics/bgInGame.png")]
public static const BgInGame:Class;

[Embed(source="../media/graphics/welcome_scarlet.png")]
public static const WelcomeScarlet:Class;

[Embed(source="../media/graphics/welcome_title.png")]
public static const WelcomeTitle:Class;

[Embed(source="../media/graphics/welcome_startButton.png")]
public static const WelcomeStartBtn:Class;

[Embed(source="../media/graphics/inGame_playButton.png")]
public static const PlayBtn:Class;

[Embed(source="../media/graphics/inGame_replayButton.png")]
public static const ReplayBtn:Class;

[Embed(source="../media/graphics/gameOver.png")]
public static const GameOver:Class;

[Embed(source="../media/graphics/present01.png")]
public static const Present01:Class;

[Embed(source="../media/graphics/sack.png")]
public static const Sack:Class;

[Embed(source="../media/graphics/snowBall.png")]
public static const SnowBall:Class;

[Embed(source="../media/graphics/laser.png")]
public static const Laser:Class;

private static var gameTextures:Dictionary = new Dictionary();
private static var gameTextureAtlas:TextureAtlas;

[Embed(source="../media/graphics/mySpritesheet.png")]
public static const AtlasTextureGame:Class;

[Embed(source="../media/graphics/mySpritesheet.xml", mimeType="application/octet-stream")]
public static const AtlasXmlGame:Class;

public static function getAtlas():TextureAtlas
{
if(gameTextureAtlas == null)
{
var texture:Texture = getTexture("AtlasTextureGame");
var xml:XML = XML(new AtlasXmlGame());
gameTextureAtlas = new TextureAtlas(texture, xml);
}
return gameTextureAtlas;
}

public static function getTexture(name:String):Texture
{
if (gameTextures[name] == undefined)
{
var bitmap:Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap(bitmap);
}
return gameTextures[name];
}
}

}

最佳答案

我想您所遵循的教程试图使一切变得非常简单,但是这样做却使它也有些混乱,因为它没有解释它的工作方式和原因。因此,让我们对其进行扩展,使您的Assets类更加灵活和有用。

您的Assets类有2个静态方法来获取和访问您的纹理和textureatlses。通过编写例如可以在任何地方访问这些方法:

Assets.getTexture("PlayBtn");

因此,让我们对声音做同样的事情。
首先,您将需要在Assets类中使用新方法,并需要新词典来保留声音引用。首先是变量(如您在此处看到的那样添加在gameTextures变量下):
private static var gameTextures:Dictionary = new Dictionary();
private static var gamesounds:Dictionary = new Dictionary();

现在该方法(在getTexture方法下添加):
public static function getSound(name:String):Sound
{
if (gamesounds[name] == undefined)
{
var sound:Sound = new Assets[name]();
gamesounds[name] = sound;
}
return gamesounds[name];
}

现在,您可以以相同的方式获得声音,无需初始化。您只需在代码中的任何地方执行此操作,例如:
var laser:Sound = Assets.getSound("LaserSound");
laser.play();

请注意,没有使用Assets.LaserSFX,如果需要,可以放心地删除它。

关于actionscript-3 - 从Assets类外部初始化八哥的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com