What I would like to do is name some jpegs in a folder and then dynamically have the name (minus the jpeg) load into a menu. I just can’t quite get my head around extracting the string of the file that I’m loading. Can anyone point me in the right direction…thank you…
// using prototype
// get file with given extension
String.prototype.getFilename = function(extension) {
return this.substr(0, this.indexOf(extension));
};
myString = "file.jpg";
trace(myString.getFilename(".jpg"));
// get file without knowing the extension
String.prototype.getFilename = function() {
return this.substr(0, this.indexOf("."));
};
myString = "file.jpg";
trace(myString.getFilename());
should help you
I really appreciate the quick response. Since I by no means am not an actionscript expert, I still feel to see the path to my directory that holds the jpegs or where that should be instituted. Maybe I should have elaborated that I want to dynamically load these jpegs via the loadMovie method and then have their file names fill a menu.
Thanks…
so its fixed ?
Nope. Basically what I would like to happen is for the jpegs names that reside in an outside folder than my swf (and will be loaded in) to dictate what my menu will read. Does this help or am I confusing you. I just noticed that the previous code you sent is php. Will that work in Actionscript.
String.prototype.getFilename = function(extension) {
return this.substr(0, this.indexOf(extension));
};
myString = “file.jpg”;
trace(myString.getFilename(".jpg"));
I can’t tell if there is a place in this code to link to the folder that holds my jpegs???
its not php, im just using the php tag to apply some colors…
yes its actionscript
so where would I insert the path to my jpegs?
You’re gonna need a serverside language to generate the filelist, and then have Flash load it.
Here’s a script I made recently, just playing around that does this. Might get you on the right track.
**** Make sure to remove the spaces in the word “files” in the preload function - the AS tag parser on this forum screwed it up.**
readdir.php
<?
header("Content-type: text/xml");
$path = ".";
$output = "<xml>
";
$output .= " <filelist>
";
if(is_dir($path)){
if($dir = opendir($path)){
while(($file = readdir($dir)) !== FALSE){
if($file != "." && $file != ".."){
if(substr($file,strlen($file)-3,3) == "jpg"){
$output .= " <file>".urlencode($file)."</file>
";
}
}
}
}
} else {
echo "<br>$path is not a valid directory.";
}
$output .= " </filelist>
";
$output .= "</xml>";
echo $output;
?>
**AS, in a new file on the main timeline:
**
function iViewer(inPos, inSafeDepth, inParent, inFadeSpeed, inPadding, inSettings, inPath, inReaddir) {
// set defaults
if (isNaN(inSafeDepth) || inSafeDepth == undefined) inSafeDepth = 1;
if (inPos.length<1) inPos = [0, 0];
if (!inParent instanceof MovieClip || inParent == undefined) inParent = _root;
if(isNaN(inFadeSpeed) || inFadeSpeed == undefined) inFadeSpeed = 3;
if(isNaN(inSpacing) || inSpacing == undefined) inSpacing = 10;
// assign properties
this.safedepth = inSafeDepth;
this.pos = {x:inPos[0], y:inPos[1]};
this.parent = inParent;
this.speed = 3;
this.fileIndex = 0;
this.files = new Array();
this.padding = inPadding;
this.settings = inSettings;
iViewer.speed = inFadeSpeed;
// initiate
if (inPath != "" && inReaddir != "") {
this.path = inPath;
this.readdir = inReaddir;
this.__init__();
} else {
trace("Error: no XML source file specified. Aborting ...");
}
}
iViewer.prototype.preload = function() {
var ref = this;
this.container._alpha = 0;
this.container.loader.loadMovie(this.path+this.files[this.fileIndex]);
this.container.onEnterFrame = function() {
if(this.loader.getBytesLoaded() == this.loader.getBytesTotal()){
if(this.loader._width > 0 && this.loader._height > 0){
delete this.onEnterFrame;
var w = this.loader._width+ref.padding*2;
var h = this.loader._height+ref.padding*2;
ref.bg.__resizeTo(w,h,MovieClip.prototype.__fadeIn,this,[]);
ref.menu.__easeX(w+10);
}
}
};
}
iViewer.prototype.buildMenu = function(){
var ref = this;
for(var i=0;i<this.files.length;i++){
var mc = this.menu.createEmptyMovieClip("menuItem"+i,i);
var bg = mc.createEmptyMovieClip("bg",1);
var border = mc.createEmptyMovieClip("border",2);
mc._y = i*20;
mc.id = i;
mc.createTextField("title",3,3,0,1,1);
tf = mc.title;
tf.autoSize = true;
tf.text = this.files*;
tf.selectable = false;
var tForm = new TextFormat();
tForm.color = 0x000000;
tForm.font = "Arial";
var w = 200;
var h = 20;
bg.lineStyle(0,0x000000,0);
bg.beginFill(0xFFFFFF,100);
bg.lineTo(w,0);
bg.lineTo(w,h);
bg.lineTo(0,h);
bg.lineTo(0,0);
bg.endFill();
border.lineStyle(0,0xCCCCCC,100);
border.lineTo(w,0);
border.lineTo(w,h);
border.lineTo(0,h);
border.lineTo(0,0);
tf.setTextFormat(tForm);
mc.onRollOver = function(){
new Color(this.bg).setRGB(0xF2F2F2);
}
mc.onRollOut = function(){
new Color(this.bg).setRGB(0xFFFFFF);
}
mc.onRelease = function(){
ref.fileIndex = this.id;
ref.container.__fadeOut(iViewer.prototype.preload,ref,[]);
}
}
}
iViewer.prototype.loadXML = function(){
var ref = this;
this.theXML = new XML();
this.theXML.ignoreWhite = true;
this.theXML.onLoad = function(ok) {
if (ok) {
var go = this.firstChild.firstChild;
for (var i = 0; i<go.childNodes.length; i++) {
ref.files* = this.firstChild.firstChild.childNodes*.firstChild.nodeValue;
}
ref.buildMenu();
ref.preload();
} else {
trace(ref.sourceURL+" could not be loaded.");
}
};
this.theXML.load(this.path+this.readdir);
}
iViewer.prototype.__init__ = function(){
this.global = this.parent.createEmptyMovieClip("iViewer", this.safedepth);
this.global._x = this.pos.x;
this.global._y = this.pos.y;
this.bg = this.global.createEmptyMovieClip("bg", 1);
this.container = this.global.createEmptyMovieClip("container", 2);
this.menu = this.global.createEmptyMovieClip("menu", 3);
this.loader = this.container.createEmptyMovieClip("loader", 1);
this.loader._x = this.loader._y = this.padding;
with (this.bg) {
lineStyle(0, 0xFF0000, 0);
beginFill(0xFFFFFF, 100);
lineTo(1, 0);
lineTo(1, 1);
lineTo(0, 1);
lineTo(0, 0);
endFill();
}
with(this.menu) {
lineStyle(0, 0xFF0000, 0);
beginFill(0xFFFFFF, 100);
lineTo(1, 0);
lineTo(1, 1);
lineTo(0, 1);
lineTo(0, 0);
endFill();
}
MovieClip.prototype.__fadeIn = function(){
this.onEnterFrame = function(){
this._alpha += iViewer.speed;
if(this._alpha >= 100){
delete this.onEnterFrame;
}
}
}
MovieClip.prototype.__fadeOut = function(flowF,flowO,flowP){
this.onEnterFrame = function(){
this._alpha -= iViewer.speed;
if(this._alpha <= 0){
delete this.onEnterFrame;
if(flowF) flowF.apply(flowO,flowP);
}
}
}
MovieClip.prototype.__resizeTo = function(w,h,flowF,flowO,flowP){
this.onEnterFrame = function(){
this._width = w-(w-this._width)/1.3;
this._height = h-(h-this._height)/1.3;
if(Math.abs(w-this._width)<1 && Math.abs(h-this._height)){
delete this.onEnterFrame;
this._width = w;
this._height = h;
if(flowF) flowF.apply(flowO,flowP);
}
}
}
MovieClip.prototype.__easeX = function(x){
this.onEnterFrame = function(){
this._x = x-(x-this._x)/1.3;
if(Math.abs(x-this._x)<1){
delete this.onEnterFrame;
this._x = x;
}
}
}
this.loadXML();
}
// start creating the viewer
viewPos = [0, 0];
viewDepth = 1;
viewParent = this;
viewSpeed = 3;
viewSpacing = 10;
Settings = new Object();
Settings.bg = new Object();
Settings.menuWidth = 200;
Settings.bg.over = 0xF1F1F1;
Settings.bg.normal = 0xFFFFFF;
Settings.bg.down = 0xF1F1F1;
Settings.txt.over = 0x000000;
Settings.txt.normal = 0x000000;
Settings.txt.down = 0x000000;
viewPath = "http://voetsjoeba.com/profile/";
viewReaddir = "readdir.php";
var myViewer = new iViewer(viewPos, viewDepth, viewParent, viewSpeed, viewSpacing, Settings, viewPath, viewReaddir);
It ain’t perfect, but it should get you started. Make sure to alter the viewPath variable in the AS - it’s the location to the folder where your images are in, and also where your PHP script is located. If you’re using another name than readdir for the php file, don’t forget to alter the viewReaddir variable.The PHP script will only work if it’s on the same server as the server specified in the viewPath.