3

在下面的代码中,只有按钮图像被嵌入到 flex 代码中。但是在 html 对象或嵌入标签中,为什么必须指定高度和宽度。即使这是一个普通按钮,如果我们不指定高度和宽度似乎有一些错误

html

<div align="center">
    <br />
    <div style="display:block;width:100px;height:100px;" id="audio" 
        class="testsound" align="center"/>
    <p class="clickable">
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
        width="400" height="100" id="myMovieName">
            <param name="movie" value="/media/players/testsound.swf"/>
            <param name="soundurl" value="/media/players/music.mp3"/>
            <param name="quality" value="high"/>
            <param name="bgcolor" value="#FFFFFF"/>
            <embed width="100" height="100" href="/media/players/testsound.swf" 
                src="/media/players/testsound.swf" 
                flashvars="soundUrl=/media/players/music.mp3" 
                quality="high" bgcolor="#FFFFFF" 
                name="myMovieName" loop="false" align=""
                type="application/x-shockwave-flash">
            </embed>
        </object>
    </p>
</div>

MXML

<?xml version="1.0"?>
<!-- embed/EmbedSound.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[

            import flash.media.*; 

            public var snd:Sound = new sndCls() as Sound; 
            public var sndChannel:SoundChannel;

            public function playSound():void {
                sndChannel=snd.play();
            }   

        ]]>
    </mx:Script>
    <mx:Image id="loader1" click="playSound();"
        source="@Embed(source='/opt/cloodon/site/media/img/speaker.gif')" />
</mx:Application>
4

3 回答 3

7

嵌入标签中的高度和宽度是必需的属性。了解一下swfobjectAdob ​​e的标签定义以及必需和可选属性

您是在问为什么需要它们,或者如果没有它们,为什么您的页面会中断(提示,第一个答案是第二个问题)?

编辑:

您应该修改 EMBED 和 OBJECT 标记的宽度和高度属性,使其与 swf 的大小完全一致。

您可以在 .mxml 中设置应用程序的宽度和高度

<mx:Application width="300" height="200">

如果将 scaleMode 设置为“noScale”,您将不会在 swf 中看到任何缩放。

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

如果您使用的是 Actionscript 3(您应该是),您可以在主类中设置元数据。

IE

[SWF(width="640", height="480")]
public class MyClass extends Sprite
{
    public function MyClass()
    {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
    }
}
于 2011-04-18T12:49:29.337 回答
1

除了@Dominic Tancredi 的评论:OBJECT 和 EMBED 标签都需要 WIDTH 和 HEIGHT 属性。

于 2011-04-18T13:16:57.763 回答
1

你应该在你的主 mxml 中指定你的宽度和高度,Application即使它只包含一个可点击的图像!
如果您希望您的页面具有 和 的弹性图像(按钮??)width: 400pxheight: 100px那么您的Application标签必须如下所示:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="100">

尽管如此,在您的 html 中,您的objectembed标签都应该具有相同的widthand height,并且您还应该为embed标签的align属性指定一个值,例如:align="middle"

您的object标签应类似于

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
    width="400" height="100" id="myMovieName"
    codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
    <param name="movie" value="/media/players/testsound.swf"/>
    <param name="soundurl" value="/media/players/music.mp3"/>
    <param name="quality" value="high"/>
    <param name="bgcolor" value="#FFFFFF"/>
    <!-- the embed tag does not have href attribute  -->
    <embed width="400" height="100"  
        src="/media/players/testsound.swf" 
        flashvars="soundUrl=/media/players/music.mp3" 
        quality="high" bgcolor="#FFFFFF" 
        name="myMovieName" loop="false" align="middle"
        type="application/x-shockwave-flash"
        pluginspage="http://www.adobe.com/go/getflashplayer">
    </embed>
</object>

其他 您的 sndClass 在哪里声明?如果您嵌入了该 mp3,则您的脚本标签应包含:

<mx:Script>
    <![CDATA[
        import mx.core.SoundAsset; 

        [Embed('/media/players/music.mp3')]
        private var _sound:Class;
        public var snd:SoundAsset = new _sound() as SoundAsset;

        public var sndChannel:SoundChannel;

        public function playSound():void {
            sndChannel=snd.play();
        }   

    ]]>
</mx:Script>

如果您想从 html 指定您的 mp3 路径,您可以查看这篇文章以供参考

更新 2

要根据您的 flex 按钮的大小设置 htmlobject的宽度和高度,您应该更新您的应用程序 mxml 代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    width="1" height="1" backgroundColor="#bee3f6"
    horizontalScrollPolicy="off" verticalScrollPolicy="off"
    creationComplete="onCreationComplete()" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            //This method will change the button's label ==> width.
            public function sayWhat():void {
                var _date:Date = new Date();
                extButton.label = "It's " + (_date.getHours() + 1) + ":" + _date.getMinutes() + 
                    ((_date.getMilliseconds() % 2) == 0 ? ". nice." : ". very tired.");
            }

            public function onCreationComplete():void {
                // binds this.sayWhat to the external interface, so when from   
                // javascript is called the compiled swf object's sayWhat function,
                // it will be transferred to this.sayWhat. 
                ExternalInterface.addCallback("sayWhat", sayWhat);

                // set the flash application's size to be exact the button's size.
                this.width = extButton.width;
                this.height = extButton.height;

                try 
                {
                    // The js function: onFlashAppInited(width, height);
                    // The next line tells the external interface (the parent application:
                    // browser window), that this application has finished loading, its 
                    // ready to be used. 
                    // We're passing the button's width and height as parameters
                    // In js there has to be a global method with this name.
                    ExternalInterface.call("onFlashAppInited", extButton.width, extButton.height);
                }
                catch (e:Error)
                {
                    trace(e.message + "\n" + e.getStackTrace(), e.name);
                }
            }        

            protected function onButtonUpdateComplete(event:FlexEvent):void
            {
                // if the button's size has changed, we notify the javascript to set the 
                // correct size to the object tag too.
                if ((this.width != extButton.width) || (this.height != extButton.height))
                {
                    // set the application size
                    this.width = extButton.width;
                    this.height = extButton.height;
                    // notify the js about the change
                    ExternalInterface.call("onFlashAppSizeChanged", this.width, this.height);
                }
            }

        ]]>             
    </mx:Script>
    <mx:Button id="extButton" label="Do something!" updateComplete="onButtonUpdateComplete(event)" />
</mx:Application>

请注意,最初此应用程序只有 1x1 像素(如果更小,则不会初始化!)。
onCreationComplete处理程序中,它的大小被更改以匹配按钮的大小。

这些值(宽度和高度)被传递给 javascript 函数onFlashAppInited,您还应该更新它:

function onFlashAppInited(width, height) {
    alert("Flash app inited!");
    appInited = true;

    onFlashAppSizeChanged(width,height);

    // remove the temporary swf container: tmp
    document.body.removeChild(document.getElementById("tmp"));
}

function onFlashAppSizeChanged(width, height) {
    flashApp.width = width + "px";
    flashApp.height = height + "px";
}
于 2011-04-19T06:47:04.633 回答