AS3 背景音效添加

 

方法一:

在flasf cs3 里的liberty 有声音 可以是 mp3  wav 等,右键选 linkage 填入类名(我这里填BTNsound),然后将它拖到场景中,发布出来(我的命名是sound.swf),放到工程src文件夹里。在flex里新建一个类(我的Loadsound.as)代码如下:
 package
{
 public class Loadsound
 {
  [Embed(source="Sound.swf",symbol="BTNsound")]//按钮
  public static var btnSound:Class;
 }
}
然后在 mxml 里
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="absolute" creationComplete="init();" >
private var Sbtn:Sound = new Loadsound.btnSound() as Sound;//按钮声音
private var soundVol:SoundTransform = new SoundTransform();//音量
internal function init():void{
      soundVol.volume = 1;
  }
private function onClink():void{
      Sbtn.play(0,0,soundVol);
}
<mx:Button x="129" y="397" label="Button" clink="onClink();"/>
OK!这种方式才是最简单的 最实用的!


方法二:

<mx:SoundEffect id="sound_effect" source="sound.mp3"
        panFrom="-1" panTo="1" loops="1" volumeFrom = "1" volumeTo="0.1" duration="3000" useDuration="false"/>

    <mx:Button x="44" y="181" label="Button" width="103" mouseDownEffect="{sound_effect}"/>
    <mx:Text x="44" y="145" text="点击鼠标,播放音效" width="129"/>

-------------------------分析------------
1 注意<mx:SoundEffect的source定义了声音的来源,和一般的Effect的target属性不同。
2 注意<mx:Button的mouseDownEffect属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用C#编程语言实现一个拼图游戏的示例代码和功能实现: ``` using System; using System.Drawing; using System.Windows.Forms; namespace PuzzleGame { public partial class Form1 : Form { private Bitmap originalImage; //原始图片 private Bitmap[] puzzleImages; //拼图图片 private PictureBox[] puzzleBoxes; //拼图框 private int gridSize = 3; //拼图格子数 private int tileSize = 0; //拼图块大小 private Point emptyTile; //空块位置 private int moves; //移动步数 private DateTime startTime; //开始时间 private bool gameStarted; //游戏开始标志 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { originalImage = new Bitmap("puzzle.jpg"); puzzleImages = new Bitmap[gridSize * gridSize]; puzzleBoxes = new PictureBox[gridSize * gridSize]; tileSize = originalImage.Width / gridSize; emptyTile = new Point(gridSize - 1, gridSize - 1); moves = 0; gameStarted = false; startTime = DateTime.Now; //切割图片 for (int row = 0; row < gridSize; row++) { for (int col = 0; col < gridSize; col++) { Bitmap tile = new Bitmap(tileSize, tileSize); Graphics g = Graphics.FromImage(tile); g.DrawImage(originalImage, new Rectangle(0, 0, tileSize, tileSize), new Rectangle(col * tileSize, row * tileSize, tileSize, tileSize), GraphicsUnit.Pixel); puzzleImages[row * gridSize + col] = tile; } } //创建拼图框 for (int i = 0; i < gridSize * gridSize; i++) { PictureBox box = new PictureBox(); box.Size = new Size(tileSize, tileSize); box.SizeMode = PictureBoxSizeMode.StretchImage; box.BorderStyle = BorderStyle.Fixed3D; box.Location = new Point(i % gridSize * tileSize, i / gridSize * tileSize); box.Image = puzzleImages[i]; box.Tag = i; box.Click += new EventHandler(box_Click); puzzleBoxes[i] = box; panel1.Controls.Add(box); } //打乱拼图顺序 Random rand = new Random(); for (int i = 0; i < gridSize * gridSize * 3; i++) { int index1 = rand.Next(gridSize * gridSize); int index2 = rand.Next(gridSize * gridSize); Bitmap temp = puzzleImages[index1]; puzzleImages[index1] = puzzleImages[index2]; puzzleImages[index2] = temp; puzzleBoxes[index1].Image = puzzleImages[index1]; puzzleBoxes[index2].Image = puzzleImages[index2]; puzzleBoxes[index1].Tag = index1; puzzleBoxes[index2].Tag = index2; } } private void box_Click(object sender, EventArgs e) { if (!gameStarted) { gameStarted = true; timer1.Start(); } PictureBox box = sender as PictureBox; int index = (int)box.Tag; Point tilePos = new Point(index % gridSize, index / gridSize); if (tilePos.X == emptyTile.X && Math.Abs(tilePos.Y - emptyTile.Y) == 1 || tilePos.Y == emptyTile.Y && Math.Abs(tilePos.X - emptyTile.X) == 1) { int emptyIndex = emptyTile.Y * gridSize + emptyTile.X; puzzleBoxes[emptyIndex].Image = puzzleImages[index]; puzzleBoxes[emptyIndex].Tag = index; box.Image = puzzleImages[emptyIndex]; box.Tag = emptyIndex; emptyTile = tilePos; moves++; if (CheckWin()) { timer1.Stop(); gameStarted = false; MessageBox.Show("恭喜您完成拼图!用时:" + timer1.Text + ",移动步数:" + moves); } } } private bool CheckWin() { for (int i = 0; i < gridSize * gridSize; i++) { if ((int)puzzleBoxes[i].Tag != i) return false; } return true; } private void timer1_Tick(object sender, EventArgs e) { TimeSpan timeElapsed = DateTime.Now - startTime; timer1.Text = timeElapsed.ToString("mm\\:ss"); } } } ``` 这个示例代码实现了拼图游戏的基本功能: 1. 加载原始图片并将其切割成多个小图片。 2. 创建拼图框,随机排列小图片。 3. 当用户点击拼图框时,判断该拼图块是否可以移动,并将移动后的拼图重新排列。 4. 记录游戏开始时间、移动步数和检查是否完成拼图。 5. 在完成拼图后,弹出提示框,告知用户用时和移动步数。 6. 添加背景音乐和拼图时的音效。 这个示例代码可以作为基础框架,您可以根据自己的需求进行定制和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值